Home
Java
String Array Examples
This page was last reviewed on Aug 29, 2023.
Dot Net Perls
String arrays. Programs handle enormous amounts of text, stored in strings. In arrays we collect and handle these strings. String array syntax is straightforward in Java.
Array
Array notes. 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.
ArrayList
Some examples. 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
Convert 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.
Tip The argument to 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 split. Often 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.
Note String.join is not called on a string instance. It is static. Be careful to pass the delimiter first, and the array second.
Note 2 The split() method returns a String array. It receives only one argument, the delimiter characters (here a comma).
String split
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
Sorting strings. By default strings are sorted alphabetically, from "a" to "z." We invoke Arrays.sort and pass it the array reference variable.
Info We import the 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
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 29, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.