Chapter 11: Java Arrays And Functions

Java Arrays

Arrays are in use to store multiple values in a single variable, or else of declaring separate variables for each value.The first element of an array starts with index zero.

Aarray Declaration:

<elementType> <arrayName>[];

Or

<elementType>[] <arrayName>;

For Example,

String[] States;

We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal – place the values in a comma-separated list, inside curly braces:

String[] States = {"Maharashtra", "Gujarat", "Kerala", "Karnataka"};

To create an array of integers, you could write:

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

Accessing Elements of an Array

You access an array element by referring to the index number.

This statement accesses the value of the first element in States:

Example
public class JavaArrayDemo {

	public static void main(String[] args) {
		String[] States = {"Maharashtra", "Gujarat", "Kerala", "Karnataka"};
		System.out.println(States[0]);
	}

}

Output:

Maharashtra

Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

How to Change an Array Element

To change the value of a specific element, refer to the index number:

Example
public class JavaArrayDemo {

	public static void main(String[] args) {
		String[] States = {"Maharashtra", "Gujarat", "Kerala", "Karnataka"};
		States[1] ="Panjab";
		System.out.println(States[1]);
	}

}

Output:

Panjab

Array Length

To find out how many elements an array has, use the length property:

Example
public class JavaArrayDemo {

	public static void main(String[] args) {
		String[] States = {"Maharashtra", "Gujarat", "Kerala", "Karnataka"};
		System.out.println(States.length);
	}

}

Output:

4

Loop Through an Array

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

The following example outputs all elements in the States array:

Example
public class JavaArrayDemo {

	public static void main(String[] args) {
		String[] States = { "Maharashtra", "Gujarat", "Kerala", "Karnataka" };
		for (int i = 0; i < States.length; i++) {
			System.out.println(States[i]);
		}
	}

}

Output:

Maharashtra
Gujarat
Kerala
Karnataka

Multidimensional Arrays

A multidimensional array is an array containing one or more arrays.

To create a two-dimensional array, add each array within its own set of curly braces:

Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };

myNumbers is now an array with two arrays as its elements.

To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers:

Example
public class JavaArrayDemo {

	public static void main(String[] args) {
		int[][] myNumbers = { {0,1,2,3}, {4,5,6} };
		int x = myNumbers[1][2];
		System.out.println(x);
		
		}
	}

Output:

 6

We can also use a for loop inside another for loop to get the elements of  array

For Example,

public class JavaArrayDemo {

	public static void main(String[] args) {
		// declaring and initializing 2D array  
		int arr[][] = { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
		// printing 2D array  
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
	}
}

Output:

1 2 3 
2 3 4 
3 4 5 

Learn about function in this video tutorial