An array is a group or the collection of data having the same data-type. Arrays are objects in Java and with the fixed size(disadvantage). Elements are arranged inside the array with index numbers starting from zero as the first elements.
Random access of any element present in the array and code optimization feature makes it more useable.
Types of array in java:
- Single Dimensional Array.
- Multidimensional Array
One Dimensional Array:
One-dimensional array in Java is an array with a bunch of values that are declared with a single index.
Array declaration:
1 2 3 4 5 | data_type [] array_Name; or data_type[] array-name or data_type array-name[]; |
Example:
1 2 3 4 | //to store integer value int intArray[]; or int []intArray; |
An array declaration has two components:
The type that is the data-type of an element and the name that is given by user choice with the brackets[].
Array initialization:
To initialize an array new is used and it appears as follows:
1 | var-name = new data-type [size]; |
Example:
1 | Array = new int[10]; |
We can also set by combining declaration and initialization in the following ways:
1 2 3 | dataType[] arrayRefVar = new dataType[arraySize]; or dataType[] arrayRefVar = {value0, value1, ..., valuek}; |
Accessing an array element:
Array elements can be accessed by using its index number, where each element is assigned by one index number.
Example:
1 | age[2] = 14; // insert 14 to third element |
Example: Click here
Multi-Dimensional Array (Jagged Array)
Declaring an array of arrays is known as Multidimensional Array or Jagged Arrays. In simple words, it is an array of arrays. It is created by appending a square brackets “[]” for each dimension. Here data are stored in a tabular form that is in row-major order.
Syntax:
1 2 3 4 5 | data-type name[size1][size2]...[sizeN]; or dataType[size1][size2]...[sizeN] arrayRefVar; or dataType [size1][size2]...[sizeN]arrayRefVar; |
Declaration:
1 2 | int twodim[5][10][4]; int threedim[5][10]; //data-type name[size1][size2]...[sizeN]; |
Instantiate Multidimensional Array in Java:
1 | int[][] arr=new int[3][3];//3 row and 3 column |
Array initialization:
1 2 3 4 5 6 7 8 9 | arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; |
We can also declare and initialize the 2D array together:
1 | int arr[][]={ {1,2,3},{4,5,6},{7,8,9} }; |
Example: Click here
Passing Array to Methods:
Similar to a variable, an array can also be passed to methods. To pass an array users need to call the array with its name and without the square roots within the method call. Let us see with an example below:
Example: Java program to sum the array values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class ArrayPassToMethodTest { public static void main(String args[]) { int arr[] = {1, 5, 3, 6, 2}; add(arr); // passing array to method add } public static void add(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) sum+=arr[i]; System.out.println("Value after addition: " + sum); } } |
Output:
1 | Value after addition: 17 |
Returning Arrays from Methods:
As array can be passed to methods, it can also be returned from the array.
The following shows an example of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class ReturningArray { public static void main(String args[]) { int array[] = method(); System.out.println("The returned array are: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } } public static int[] method() { // returning 1D array return new int[]{4,5,6}; } } |
Output:
1 2 | The returned array are: 4 5 6 |
The foreach Loops:
It is also possible to print the array using its for-each loop. for-each loop prints the array elements one by one, by holding an array element in a variable, then executing the body of the loop.
Example of foreach loop in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class ArrayWithForEachTest { public static void main(String[] args) { int[] arr = {1, 2, 3, 4}; System.out.println("Elements in array are:"); // Print all the array elements for (int element: arr) { System.out.println(element); } } } |
Output:
1 2 3 4 5 | Elements in array are: 1 2 3 4 |