Explore various methods of string array assignment
In programming, processing strings is a very common operation. When processing strings, it is often necessary to use arrays to store and manage multiple strings. This article will explore various methods of string array assignment and give specific code examples.
String[] names = new String[3]; names[0] = "Tom"; names[1] = "Jerry"; names[2] = "Spike";
String[] names = {"Tom", "Jerry", "Spike"};
String[] names = new String[3]; for (int i = 0; i < names.length; i++) { names[i] = "Name" + i; }
String[] source = {"Tom", "Jerry", "Spike"}; String[] target = new String[source.length]; System.arraycopy(source, 0, target, 0, source.length);
String[] source = {"Tom", "Jerry", "Spike"}; String[] target = Arrays.copyOf(source, source.length);
ArrayList<String> list = new ArrayList<>(); list.add("Tom"); list.add("Jerry"); list.add("Spike"); String[] names = list.toArray(new String[list.size()]);
Summary:
This article introduces common string array assignment methods and gives specific code examples. Whether it is the direct assignment method, the string literal method, or the use of loops, System.arraycopy() method, Arrays.copyOf() method, and ArrayList's toArray() method, assignment to string arrays can be effectively achieved. According to actual needs, choosing the appropriate method to operate can improve the readability and efficiency of the code.
The above is the detailed content of Various methods to parse the assignment of string arrays. For more information, please follow other related articles on the PHP Chinese website!