Arrays

What are arrays?

Arrays are containers of more than one variable of a given type. Think of an array as a list of objects. In the first example below an array of int variables is created and called nums. First you create a reference to an int array (int[]) using the square brackets after the data type to indicate that this is an array. Next you create the array with the new operator and set the nums reference varable to it. Now you can store values in or read values from the variables in this array by using the index into the array.




You can also create arrays of reference variables. In the example below an array of five reference variables to Dog objects is created. Note that we must create the array with the new operator, then we must create each of the Dog objects also with the new operator.