Outside Resources:
TheNewBoston – Intro to Arrays
Java has many types of collections. The most basic is the array.
The example below creates an array of Strings, adds elements to the array, and loops through the array.
//Declare a String array (notice square brackets[]) String[] things; //Create array of 4 Strings //(starts empty) //(can't increase maximum size later) things = new String[4]; //add some Strings to the array things[0] = "pizza"; things[1] = "spatula"; things[2] = "car"; things[3] = "irony"; //Can't have a things[4] - not enough room //regular for loop to print each item //Notice "things[i]" --> i is 0, 1, 2, 3 for (int i=0; i<4; i++){ System.out.println(things[i]); } //for each loop to print each item //variable s becomes each String in the array for (String s: things){ System.out.println(s); }