import java.util.Arrays; public class ArrayExample { public static void main(String[] args) { // array of strings /* * String[] names = new String[5]; names[0] = "Sara"; names[1] = * "Susan"; names[2] = "Lam"; names[3] = "Aaron"; names[4] = "Chris"; */ // another way String[] names = { "Sara", "Susan", "Lam", "Aaron", "Chris" }; // Print the array with the format // Sara, Susan, Lam, Aaron, Chris // names.length is the number of elements in the array // here 5 printArray(names); // can also use the Arrays class // the method toString in Arrays returns a string // representation of the given array String s = Arrays.toString(names); System.out.println("\n" + s); } /** * prints the given array with the format * s[0], s[1], s[2], ..., s[s.length - 1] */ public static void printArray(String[] s) { for (int i = 0; i < s.length; i++) { if (i < s.length - 1) { System.out.print(s[i] + ", "); } else { System.out.print(s[i]); } } } }