Skip to content
trekitch edited this page Apr 22, 2017 · 10 revisions

Arrays are useful when you have multiple values that you want to store under a single variable. For example, let's say you wanted to store the ages of students. I could make an array for ages and store the ages in it.

Declaring Arrays

Declaring arrays is similar to declaring variables. Just like variables you have to give it a data type and a name.

Example:

int[] ages;

Take notice of the the square brackets, [ ] , next to the data type. This tells the compiler that this is going to be an array and not just a single integer.By default arrays are empty but just like you can initialize variables you can also initialize arrays. However, there are different ways to initialize arrays. One way is that you could give the array specific values.

Example:

int[] ages = {2,4,6,8,10};

Another way to initialize an array is to just specify the size of the array. This is most likely that way you will create arrays in the future because you may not always know the value to put into the array.

Example:

int[]  testScore = new int[10];

Note about arrays

It is important to note the arrays are 0 indexed. Every item in an array has an index number which acts like an id for each number. With arrays in java, the index starts off at 0. If you were to make a table of the ages index it would look like this:

Index position Number
ages[0] 2
ages[1] 4
ages[2] 6
ages[3] 8
ages[4] 10

Notice how the index is specified in the table. This basically says that this is the number that is in the 0 position in the ages array.

Processing arrays

As I said before arrays are useful for storing multiple values. When it is time to access these value you can just use the index for the array. For example, if you wanted to output the value in the third position of the array.

int[] ages = {2,4,6,8,10};
        
System.out.println(ages[3]);

Output

8

You can manipulate arrays in various ways. Arrays can also be as long or as short as you want.

For each loop

While working with arrays you will need to sometimes get the contents of the array. To do this you can use what is called a foreach loop. A foreach loop is similar to a for loop it is even called an enhanced for loop by some. Foreach loops are only used to output the contents of the array mostly.

Example:

int[] ages = {2,4,6,8,10};

for(int i:ages){
System.out.println(i);
}

Output

2
4
6
8
10

Above you can see that for the foreach loop first a variable is declared and initialized and then it is given an array to process. Java knows that I am using a foreach loop and then goes through the ages array and output every number at position i.

Multidimensional Arrays

Arrays with only one set of numbers are known as single dimensional arrays. However, you can have multidimensional arrays to hold multiple related numbers. Let's say I wanted a two-dimensional array:

int[][] numbersArray = [5][5];

This code creates an array with 5 rows and 5 columns. If you want to store values in the array then you would do it similarly to storing it in a single dimensional array:

numbersArray[0][0] = 25;
numbersArray[1][1] = 50;
numbersArray[2][2] = 75;
numbersArray[3][3] = 100;
numbersArray[4][4] = 125;

This code stores the number 25 into the 0,0 index position of the array. Multidimensional arrays are not limited to two dimensions though. Arrays can have as many as 32 dimensions. However, as you add more dimensions the more complicated the array gets. To help visualization the array would look like this.

25 0 0 0 0
0 50 0 0 0
0 0 75 0 0
0 0 0 100 0
0 0 0 0 125

Multidimensional arrays can be used for various relationship tasks. For example, let's say you are a teacher that has 5 students and each student has to take 5 tests. Then you could make a 5 x 5 array to store this. At position [0][0] would be the first students first score.

Jagged Arrays

A jagged array is basically an array of arrays. This allows you to have a collection of arrays that are a different length. If you wanted to make an array of different arrays you would do this:

int[][] jaggedArray = new int[3][];

Now if you want to set the elements in the array you would do it just like you would for a regular array.

jaggedArray[0] = new int[] {5,6,89};
jaggedArray[1] = new int[] {1,2,3,4,5,6};
jaggedArray[2] = new int[] {100,90,80,50,10};

To access the elements of a jagged array you first need to specify which array you want to access and then what position in the array to access.

//access the 3 position in the first array
System.out.println(jaggedArray[1][3]);

Output

4
Clone this wiki locally