Arrays
An array is a data structure that allows you to essentially set up a table of like variables.
An example where an array is advantageous is the situation where you may want to enter 20 marks for a student. In the past you would need to define 20 variables for the marks (i.e. mark1, mark2, mark3, mark4, … mark20). An array allows you to define all 20 variables in one line of code.
int[] marks = new int[20];
The first line tells the Java compiler that we would like to make an array that will hold integers. The second part is the name of our new array that we can use to reference each element. The new statement is similar to using the Board. It is telling Java that we need to make some room to store some integers for our array and the int[20] specifies that we specifically need 20 spots for our array.
We can define an array of any type in Java by following the same format
e.g.
double[] someDoubles = new double[50];
Robot[] bots = new Robots[3];
char[] characters = new char[5];
All of these statements will create new arrays of varying types. Notice that these arrays have not been told what values they are storing. The above statements only tell Java to make room, not to actually store any values inside of these containers. To do this, we tell Java which location we want to use, and what should be stored in that location. We start counting elements at 0 (0 is the 1st spot available in our array. An array of size 5 has elements 0 through 4).
marks[0] = 58;
marks[1] = 45;
marks[2] = 87;
The first statement tells Java at location 0 inside of the marks array we want to hold the integer value of 58.
If we have a relatively small array, we could also assign the values using a special operator when we create the variable.
int[] littleArray = {3,6,7,8};
This creates an array with littleArray[0] = 3, littleArray[1] = 6, littleArray[2] = 7, and littleArray[3] = 8.
Sometimes we might be able to assign values to an array using a specific pattern or mathematical formula. For instance, maybe we want each spot in the array to hold its array position (0th spot holds 0, 1 holds 1, etc.). For this, we can use a for loop!
Arrays have a special property that can tell us how big they are. This is the .length property. For example, littleArray.length would return the number 4. Marks.length would return 20 because that is the size we told it when we created it. We can use this in our for loop.
int[] test = new int[25];
for(int I = 0; i < test.length; i++)
{
test[i] = i;
}
This will loop through every element in the array and set its value to be the position it is on. We can also use the same loop structure to read back all the elements in the array:
for(int I = 0; i < test.length; i++)
{
System.out.println(test[i]);
}
Again, this loop will go through each element and print out the value it stores. Sometimes we will use more than one array to store values that relate to each other. For instance, we could store names in one array and their phone numbers in another. The position in each array would relate to each other; it would be the person’s name and their phone number. This construction is known as related arrays.