String
arraysPrograms handle enormous amounts of text, stored in strings. In arrays we collect and handle these strings. String
array syntax is straightforward in Java.
In Java collections like ArrayLists
are built on top of arrays. An array is the most low-level way to store elements together. It won't resize automatically.
This program creates two string
arrays. It first uses array initializer syntax to create a three-element array in one line. It prints the length and elements from the array.
public class Program { public static void main(String[] args) { // Create three-element String array. String[] elements = { "cat", "dog", "mouse" }; // Print element count and first element. System.out.println(elements.length); System.out.println(elements[0]); // Loop over strings in the list with for-each loop. for (String element : elements) { System.out.println(element); } // Use a longer syntax form for creating the array. String[] elements2 = new String[] { "bird", "fish", "cow" }; System.out.println(elements2.length); // Loop over string indexes with for-loop. for (int i = 0; i < elements2.length; i++) { System.out.println(elements2[i]); } } }3 cat cat dog mouse 3 bird fish cow
ArrayList
Often in programs, an ArrayList
is more convenient and easy-to-use. But sometimes we must convert one to a String
array. This is easy with toArray
.
toArray
on ArrayList
can be any String
array, even a zero-element one.import java.util.ArrayList; public class Program { public static void main(String[] args) { // Create an ArrayList of strings. ArrayList<String> list = new ArrayList<>(); list.add("cat"); list.add("box"); list.add("elephant"); // Use toArray to copy ArrayList to string array. String[] array = new String[list.size()]; array = list.toArray(array); // Loop over the string elements in the array. for (String item : array) { System.out.println(item); } } }cat box elephant
Join
and splitOften we combine strings in an array together (with String.join
) and often we separate a string
apart (with split). We specify a delimiter character with both methods.
String.join
is not called on a string
instance. It is static
. Be careful to pass the delimiter first, and the array second.split()
method returns a String
array. It receives only one argument, the delimiter characters (here a comma).public class Program { public static void main(String[] args) { // This string array contains three values. String[] values = { "cat", "dog", "spider" }; // Combine these strings together. String joined = String.join(",", values); System.out.println(joined); // Separate the combined string with split. String[] values2 = joined.split(","); for (String v : values2) { System.out.println(v); } } }cat,dog,spider cat dog spider
By default strings are sorted alphabetically, from "a" to "z." We invoke Arrays.sort
and pass it the array reference variable.
java.util.Arrays
package to easily reference the Arrays.sort
method. Sort
acts in-place.import java.util.Arrays; public class Program { public static void main(String[] args) { // Create a string array with four values. String[] values = new String[4]; values[0] = "zoo"; values[1] = "marina"; values[2] = "amphitheater"; values[3] = "colosseum"; // Sort the strings. Arrays.sort(values); // Display the sorted strings. for (String v : values) { System.out.println(v); } } }amphitheater colosseum marina zoo
String
arrays are a way to collect, and handle, groups of strings together. Common array methods can be used with string
arrays. We handle large amounts of text.