Arrays

In Java, an array is an object that contains fixed set of items that are of same type.

Declaration and Initialization of an Array

Array variables of type primitive are different than ordinary primitive variables. An ordinary primitive variable will hold a value in its allocated memory location. Whereas, an Array variable, which is a reference type variable, hold the memory address of an Array object.

int[] arr1 = new int[5];

Here, an Array object reference variable is declared and initialized with an Array object of size 5. All of the elements in that object will be of type int and are initialized to 0.

int[] arr2 = {1, 2, 3, 4, 5};

If the elements of an Array are predetermined, you may declare it as above. Here, an Array object reference variable is declared and initialized with 5 element Array object.

int[] arr3 = null;

Here, an Array object reference variable is declared with null, that is, this reference variable is not yet pointing to any Array object.

Accessing an element of an Array

int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[2]);
arr[2] = 30;

To access an individual element from an Array, we need to use index of that element within square brackets. An index of an Array is like a serial number, but it will start with 0. So, in the above code snippet, arr[2] will return 3.

Length of an Array

Array objects are provided with an attribute length which returns the size of an Array.

int[] arr1 = {1, 2, 3, 4, 5};
System.out.println(arr.length);
5

Iterate over an Array

Yet times you may want to iterate over an array to process individual element containing in that array. In order to do that, we can use a loop as below:

package com.techstackjournal;

public class Arrays {
	public static void main(String[] args) {

		int[] arr = { 4, 66, 31, 37, 40 };

		for (int i = 0; i < arr.length; i++) {
			if (arr[i] % 2 == 0) {
				System.out.println(arr[i] + " is even number");
			} else {
				System.out.println(arr[i] + " is odd number");
			}
		}

	}
}
4 is even number
66 is even number
31 is odd number
37 is odd number
40 is even number
iarr.lengthi<arr.lengtharr[i]arr[i] % 2 == 0Printi++
05true4true4 is even number1
15true66true66 is even number2
25true31false31 is odd number3
35true37false37 is odd number4
45true40true40 is even number5
55falseNENENENE
NE – Not Executed

ArrayIndexOutOfBoundsException

Trying to access a non existent Array element will throw an Exception called as ArrayIndexOutOfBoundsException. In Java, JVM signals an error in the form of an exception, when it finds violation of semantic constraints of the programming language.

So, whenever you come across this kind of exception check the flow of your program and identify where exactly it is referring non-existent array element.

package com.techstackjournal;

public class Arrays {
	public static void main(String[] args) {

		int[] arr = { 10, 20, 30, 40, 50 };

		for (int i = 0; i <= arr.length; i++) {
			System.out.println(arr[i]);
		}

	}
}
10
20
30
40
50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
	at com.techstackjournal.Arrays.main(Arrays.java:9)

Above program throws ArrayIndexOutOfBoundsException as it tries to access element at index 6 which is non-existent.