Java Arrays

 

An array in Java is a data structure that acts as a container to hold a fixed size of variables of the same type in a sequential way. Each variable in an array is called an element of the array. The number of elements in an array is determined at the time of creation. Arrays are immutable and cannot be changed once they are created. However, the elements of the array can be assigned different values.

An array in Java is declared and initialized as follows:

<variable type> <variable_name> [] = new <variable type>[<array size>];

Alternatively, the brackets can go after the variable name in the declaration.

<variable type> [] <variable_name> = new <variable type>[<array size>];

All the variables in the array are required to be of the same type.

Array is an object type and it can be a container for objects as well as primitives.

Example for an int array;

int [] a = new int[10];

Example for an object array;

Object [] b = new Object[10];

Once the array is created, all the elements of the array are initialized to their default values. In other words, if it’s an array of objects, all the elements of the array are initialized with null values. If it’s an array of primitives, all the variables in the array are initialized to the default value for the primitive type following initialization. For example, if it’s an integer array, all the variables are initialized to 0.

For example;

int [i] = new int[10];

This initializes all the 10 integers in the array to 0.

The number of elements in an array is referred to as the length of the array. The length of an array property returns the number of elements in the array.

For example, the following snippet of code would print 10 on the console.

int [i] n = new int[10];

System.out.println(n.length);

Accessing the elements of an array

The position of an array element inside the array is called the “index” of that element. In an array, the index starts from 0 and goes up to one less than the number of elements in the array. Though arrays are immutable, the element assigned to a specific index in an array can be changed. An element of an array is accessed as follows:

<array variable name>[<index of the element]

For example, if we define an array of integers as follows

int n [] = new int[10];

element at index 3 can be accessed by n[3]. Using this, the the element at index 3 can be assigned new values or its value can be read and used.

Let’s see how this works with an example.

public class MyClass {

public static void main(String [] args) {

int intArray[] = new intArray[10];

System.out.println(“Element at index 2 is “ + intArray[2]);

intArray[2] = 5;

System.out.println(“Element at index 2 after assigning is “ + intArray[2]);

}

}

The output would be

The element at index 2 is 0

The element at index 2 after assigning is 5

Here in this example, an integer array variable, intArray, is declared and initialized with length 10. Initially, all the elements of the array are initialized with value 0, since this is an integer array. Therefore, intArray[2] has the value 0 and this gets printed on the console on line 4. Following that, intArray[2] is set to 5, and the value is printed on the console once again.

Looping over the elements of an array

Looping over the elements of an array for reading or writing is a common practice in Java. It requires less number of codes compared to accessing all the elements of the array individually and it’s much cleaner. All of the for, while and do-while loops can be used while looping over an array. for loops by far used to be the most preferred way for Java developers to loop over arrays. However, for-each loops introduced with Java 5 became the de-facto standard of looping over arrays later on.

Let’s see how we can loop over an array with an example.

public class MyClass {

public static void main(String [] args) {

int intArray[] = new int[3];

for (int i = 0; i < intArray.length; i++) {

System.out.println(intArray[i]);

}

int j = 0;

while (j < intArray.length) {

intArray[j] = (j + 1);

j++;

}

int k = 0;

do {

System.out.println(intArray[k]);

k++;

} while {k < intArray.length);

}

}

The output would be

0

0

0

1

2

3

In this example, we first create an int array of length 3. Following that, we loop over the array using a for loop and print all the elements in an orderly way. Because the elements of the array are initialized to 0 by default after initialization of the array, all the printed values are 0. Following this, we set the values in the array using a while loop and the j variable. We define the j variable outside the loop, increment it by 1 with each loop, and in each loop we set the value at the given index to 1 more than the index. The last loop is a do-while loop. This time, we define an integer k outside the loop for control and initialize it to 0. Because do-while loops are executed at least once, the termination condition is k < intArray.length while we increase k with every loop. After k becomes 3, the termination condition evaluates to false and the program flow exits the do-while loop.

for-each loops are a variation of for loops and they don’t keep the control of the index, but instead, using the order in the array, they provide looping over each element in the array. for-each loops are defined as follows:

for (<type> <variable name> : <array name>) {

//do something with <variable name>

}

Let’s see how we can loop over an array using a for-each loop with an example.

public class MyClass {

public static void main(String [] args) {

int intArray [] = new int[3];

for (int i = 0; i < intArray.length; i++) {

intArray[i] = i * 2;

}

int sum = 0;

for (int i : intArray) {

System.out.println(i);

sum += i;

}

System.out.println(“The sum is “ : + sum);

}

}

The output would be

0

2

4

The sum is 6

Here in this example, we first define an integer array of length 3 and then set all the elements to twice their indexes. Following this, we first define an integer, sum, outside the loop. Then we define a for-each loop to loop over the elements of the array, in the index order 0, 1, 2 and with each loop, we increment the sum by the value of the element at the current index. After looping over all the elements of the array, the sum is printed on the console.

%d bloggers like this: