Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Array Creation and Access

To keep track of 10 exam scores, we could declare 10 separate variables: int score1, score2, score3, … , score10. But what if we had 100 exam scores? That would be a lot of variables! Most programming languages have a simple data structure for a collection of related data that makes this easier. In Python, this is called a list. In Java and many programming languages, this is called an array.

An array is a block of memory that stores a collection of data items (elements) of the same type under one name. Arrays are useful whenever you have many elements of data of the same type that you want to keep track of, but you don’t need to name each one. Instead, you use the array name and a number (called an index) for the position of an element in the array. You can make arrays of ints, doubles, Strings, and even classes that you have written, such as, Students.

Arrays and lists in most programming languages start counting elements from the number 0, so the first element in an array is at index 0. This is similar to how Strings are indexed in Java – the first character is at index 0.

Declaring and Creating an Array

When we declare a variable, we specify its type and then the variable name. To idicate that a variable is an array, we place square brackets after the data type. This data type will be for all the elements in the array.

int score;           // Declaration for a single int variable
int[] scores;        // Declaration for an array of ints


To create (define) the array after declaring the array variable, use the new keyword with the type and the size of the array (the number of elements it can hold). This will create the array in memory. You can do the declaration and the creation (definition) all in one step. See the String array names below. The size of an array is set at the time of creation and cannot be changed after that.

int[] highScores;                   // declare an array variable

highScores = new int[5];            // create (define) the array

String[] names = new String[5];     // declare and create (define) array in one step

Initializer Lists

Another way to create an array is to use an initializer list. You can initialize (set) the values in the array to a list of values specified in curly brackets { } when you create it, like below. In this case you don’t specify the size of the array; the array size will be determined from the number of values that you specify in the list.

int[ ] highScores = {99, 98, 98, 88, 68};
String[ ] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"};

When you create an array of a primitive type (for example, int) with initial values specified, space in memory is allocated for the specified number of items of that type, and the values in the array are set to the specified values. When you create an array of an object type (such as, String) with initial values, space in memory is set aside for that number of object references. The objects are created (on the heap) and the object references set so that the objects can be found.

In the example below, highScores is an array of int’s, and names is an array of String objects, or more precisely, an array of references to String objects.

Arrays know their length (how many elements they can store). The length of the array is stored in a public, read-only instance variable, in the array object. You can use dot-notation to access the instance variable (arrayName.length). Dot-notation is using variable name followed by a . and then the instance variable (property) name or a method name. Try the following.

Access and Modify Array Values

To access the elements in an array, we use an indexed array variable which is the array name and the index inside square brackets [ ]. Remember that an index is a number that indicates the position of an element in the array, starting at 0. You can think of the index as an offset from the beginning of the array.

highScores[0] = 99;                    // assign a new value 99 to the first element in the array

System.out.println( highScores[0] );   // print the first element of the array

Programming Challenge: Countries

Create 3 parallel arrays and initialize them using initialization lists that represent the data below. Remember that the order of these arrays has to match so that you can use the same index to access corresponding values.

  • Countries: China, Egypt, France, Germany, India, Japan, Kenya, Mexico, United Kingdom, United States
  • Capitals: Beijing, Cairo, Paris, Berlin, New Delhi, Tokyo, Nairobi, Mexico City, London, Washington D.C.
  • Languages: Chinese, Arabic, French, German, Hindi, Japanese, Swahili, Spanish, English, English

Your print statement should print something like this:

China's main language is Chinese and its capital city is Beijing.