Collection of similar type of elements is known as Array. Array in Java is an Object that holds fixed number of values of a similar data types which means an array of int will contain only integers, an array of string will contain only strings etc.. The length of an array is established when the array is created. After creation, its length is fixed. Array is a index based and its index starts from 0 which means the first element of an array is stored at 0 index. Array holds primitive types as well as object references.

Syntax:

1
2
dataType[] arrayName;
arrayName  = new dataType[arraySize];

or

1
arrayName[index] = arrayElement;

Example:

1
2
int [] arr;
arr = new int[5];

also

1
int[] arr = new int[5];

‘arr’ is a collection of 5 integers

int[] arr represents that ‘arr’ is an array of type integer
[] represents an array
new int[5] represents it as array of 5 integers

As per the above example, we have 5 elements in the array such as 11, 22, 33, 44, 55.

Arrays index starts from 0 so the first element (11) of this array has index 0, second element (22) has index 1, … last element (55 which is fifth element) has index of 4.

Which means,

arr(0) = 11;
arr(1) = 22;
arr(2) = 33;
arr(3) = 44;
arr(4) = 55;

If we know the elements of an array, then we can also declare the array and assign values to the its variables as:

1
int[5] arr = {11,22,33,44,55}

Array Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package classFiveArrays;
 
import java.util.Scanner;
 
public class ArrayClass {
 
public static void main(String[] args){
  
int[] arr = {11,22,33,44,55};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
  }
}

Output:

1
2
3
4
5
11
22
33
44
55

Array Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package classFiveArrays;
 
import java.util.Scanner;
 
public class ArrayClass {
 
public static void main(String[] args){
  
int[] arr = new int[5];
arr[0] = 11;
arr[1] = 22;
arr[2] = 33;
arr[3] = 44;
arr[4] = 55;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
  }
}

Output:

1
2
3
4
5
11
22
33
44
55

Finding Length of Array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package classFiveArrays;
 
import java.util.Scanner;
 
public class ArrayClass {
 
public static void main(String[] args){
// To find the length of an array
int[] arr = new int[5];
// ‘length’ is a function in Java which gives you the size of an array
System.out.println(arr.length);
  }
}

Output:

1
5

Array with For Loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package classFiveArrays;
 
public class ArrayForClass {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
for(int i=0; i<arr.length; i++){
System.out.println(arr[i]);
}
 
}
 
}

Output:

1
2
3
4
5
11
22
33
44
55

Array with For Each Loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package classFiveArrays;
 
public class ArrayEnhancedForClass {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
for(int a:arr){
System.out.println(a);
}
 
}
 
}

Here, the variable a will go to every element of the array arr and will take its value.
So, in the first iteration, a is the 1st element of the array arr i.e. 11.
In the second iteration, it is the 2nd element i.e. 22 and so on.

Output:

1
2
3
4
5
11
22
33
44
55

2D Array:

2D Arrays are same like 1D Array but it consists of rows and columns.

2D Array Declaration:

Syntax:

1
dataType[] arrayName = new dataType[row][column];

Example:

1
int[][] arr = new int[2][3];

Here, arr is a 2-D array of type int which consists of 2 rows and 3 columns.

Lets see how the rows and columns work

2D Array Initialization:

Declaring 2D array and then assigning values to its elements

1
2
3
4
5
6
7
int[][] a = new int[2][3];
a[0][0]=11;
a[0][1]=22;
a[0][2]=33;
a[1][0]=44;
a[1][1]=55;
a[1][2]=66;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package classFiveArrays;
 
public class Array2DClass {
public static void main (String [] args){
int[][] arr = new int[2][3];
arr[0][0]=11;
arr[0][1]=22;
arr[0][2]=33;
arr[1][0]=44;
arr[1][1]=55;
arr[1][2]=66;
System.out.println(arr[0][0]);
System.out.println(arr[0][1]);
System.out.println(arr[0][2]);
System.out.println(arr[1][0]);
System.out.println(arr[1][1]);
System.out.println(arr[1][2]);
 
}
 
}

Output:

1
2
3
4
5
6
11
22
33
44
55
66

Declaring and assigning values at the same time

1
2
3
4
int a[][] = {
  {11, 22, 33},
  {44, 55, 66 }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package classFiveArrays;
 
public class Array2DClass {
public static void main (String [] args){
int[][] arr= {
{11, 22, 33},
{44, 55, 66}
};
 
System.out.println(arr[0][0]);
System.out.println(arr[0][1]);
System.out.println(arr[0][2]);
System.out.println(arr[1][0]);
System.out.println(arr[1][1]);
System.out.println(arr[1][2]);
 
}
 
}

Here, the value of a[0][0] is 11, a[0][1] is 22, a[0][2] is 33, a[1][0] is 44, a[1][1] is 55 and a[1][2] is 66.

Output:

1
2
3
4
5
6
11
22
33
44
55
66

Limitations of Array:

1. Arrays wont allow multiple data type.
2. In Array, we can store only fixed size of elements.

To overcome this problem, we can use ArrayList , List, Set, Collections. We will see those in next sections.

Must Read: Java Tutorial