Array

Definition

An array is a structure used to store multiple pieces of data. All data stored in an array must be of the same type.

Using Arrays

Declaration

Declaring an array is very similar to declaring a regular variable. You must include the data type which you will be storing in the array and you must name the array:

int[] x;

Initialization

There are two ways to initialize an array. The first is to assign a literal array:

int[] x = {2,4,6};

Using this method of initialization you create an array with the elements already filled in.

The second method for initializing an array is to assign an "empty" array of size n:

int[] x = new int[n];

This gives you an array of length n in which each index is occupied by the default value of the array's type.

Array Indicies

Arrays start at index 0 and increase by 1 for every spot to the right. For example, the first element in an array x is x[0] and the second is x[1].

The last element in the array is always x[x.length-1] since the first index is 0.

Retrieving an Array Element's Value

You can retrieve a value from an array's element by subscripting. For example:

int n ;
int[] x = {2,4,6};
n = x[0];

We read the third line as, "n equals x sub zero." Now n is equal to 2.

Changing an Array Element's Value

To change an elements value we use subscripting again. Let's say you wanted the first elements of an array to be 9. You would do the following:

x[0] = 9;

Arrays and Loops

Loops can be used to easily manipulate arrays in a number of ways. If you have declared an empty array int[] x of length n a simple for loop can be used to assign numbers to each element of the array.

For example, this is how you would fill an array starting with 1 up until the length of the array:

int[] x = new int[n];
for(int i = 0; i < x.length; i++){
    x[i] = i + 1;
}

Arrays generally work very well with for loops.

++Printing Arrays

public static void printArray(int[] toPrint){ 
            System.out.print("["); 
            for (int i = 0; i < toPrint.length - 1; i++) { 
                  System.out.print(toPrint[i] + ", "); 
            } 
            System.out.println(toPrint[toPrint.length - 1] + "]"); 
      }

Multidimensional Arrays

Declaration

Declaring a multidimensional array is very similar to declaring a regular array. For example, to declare a two-dimensional array you must include the data type which you will be storing in the array and you must name the array. Just this time, you use 2 brackets:

int[][] x;

Initialization

There are two ways to initialize a multidimensional array. The first is to assign nested literal arrays:

int[][] x = {{1,3,5,4},{2,7,7,8},{5.9.3,4}};

This Array would look like:
1, 3, 5, 4
2, 7, 7, 8
5, 9, 3, 4

Using this method of initialization you create an array with the elements already filled in.

The second method for initializing a multidimensional array is to assign an "empty" array with i rows and j columns:

int[][] x = new int[i][j];

This gives you an array with i rows and j columns in which each index is occupied by the default value of the array's type.

Retrieving a Multidimensional Array Element's Value

You can retrieve a value from a multidimensional array's element by subscripting. For example:

int n ;
int[][] x = {{1,3,5,4},{2,7,7,8},{5.9.3,4}};
n = x[2][1];

1, 3, 5, 4
2, 7, 7, 8
5, 9, 3, 4

We read the third line as, "n equals x: row, column." Now n is equal to 9.
n = x[0][3] would make n equal to 4

Changing an Array Element's Value

To change an elements value we use subscripting again. Let's say you wanted the first elements of a multidimensional array to be 3. You would do the following:

x[0][0] = 3;

Printing Multidimensional Arrays

public static void printMultiDimensionalArray(int[][] toPrint){
    for (int i = 0; i < toPrint.length; i++) {
        for (int j = 0; j <  toPrint[i].length; j++) {               
              System.out.print(" " + toPrint[i][j]);
        }
        System.out.println(""); 
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License