Array in Java

  • An array is an object that is used to store multiple values of the same type. In other words, it is used to store homogeneous values. It can store two types of data- Primitive values and Objects. The members of an array are defined in contiguous memory location.
  • The length of an array cannot be changed after instantiation. You can use variable length to determine the number of elements in an array. Don't confuse yourself with a method length() which is used by String to get the length of String. Example-
    int scores[] = {14, 15, 16}; System.out.println("Length of array is " + scores.length);
  • An array in Java uses zero-based indexing which means the first element in every array has index zero. So, the elements of array scores are scores[0], scores[1], scores[2] and so on.
  • You can declare array of the following types-
    • Primitive data type- eg int scores[];
    • Class- eg String names[];
    • Interface-
      interface MyInterface{}					
      MyInterface arr[];
    • Abstract class-
      abstract class MyAbstract{}
      MyAbstract arr[];

Types of Array in Java

Java provides two types of array-

One-Dimensional Array in Java

How to declare an array in Java

arrayType[] arrayReferenceVariable; (or)
arrayType []arrayReferenceVariable; (or)
arrayType arrayReferenceVariable[];

Example of array declaration- int scores[];
You cannot write the size of array in an array declaration, doing so will give compile-time error. Eg- int arr[5]; will give compile-time error.

How to instantiate an array in Java

arrayReferenceVariable = new arrayType[size];

Example of array instantiation- scores = new int[6];
You know that array is an object that is why it is instantiated with the new keyword. If you forget to specify the size of the array then you will get compile-time error.

How to declare and instantiate an array at the same time

arrayType[] arrayReferenceVariable = new arrayType[size]; (or)
arrayType []arrayReferenceVariable = new arrayType[size]; (or)
arrayType arrayReferenceVariable[] = new arrayType[size];

How to initialize an array in Java

arrayType[] arrayReferenceVariable = {value1, value2, ..., valuen}; (or)
arrayType[] arrayReferenceVariable = new arrayType[]{value1, value2, ..., valuen};

Example of single dimensional array in Java

class Example
{
	public static void main(String args[])
	{
		int i = new int[3];
		i[0] = 5;
		i[1] = 10;
		i[2] = 15;
		for(int j=0; j<i.length; j++)
		{
			System.out.println(i[j]);
		}
	}
}

Output

5
10
15

Multidimensional Array in Java

How to declare multidimensional array in Java

arrayType[][] arrayReferenceVariable; (or)
arrayType [][]arrayReferenceVariable; (or)
arrayType arrayReferenceVariable[][]; (or)
arrayType[] arrayReferenceVariable[]; (or)
arrayType []arrayReferenceVariable[];

Example of multidimensional array declaration- int matrix[][];
You cannot write the size of array in an array declaration, doing so will give compile-time error. Eg- int matrix[3][3]; will give compile-time error.

How to instantiate multidimensional array in Java

arrayReferenceVariable = new arrayType[rowsize][columnsize];

Example of array instantiation- matrix = new int[3][3];
Here, we will obtain a multidimensional array matrix having 3 rows and 3 columns. With the help of new keyword, you instantiate multidimensional array. If you forget to specify the size of row and column then you will get compile-time error.

How to declare and instantiate multidimensional array at the same time

arrayType[][] arrayReferenceVariable = new arrayType[rowsize][columnsize]; (or)
arrayType [][]arrayReferenceVariable = new arrayType[rowsize][columnsize]; (or)
arrayType arrayReferenceVariable[][] = new arrayType[rowsize][columnsize]; (or)
arrayType[] arrayReferenceVariable[] = new arrayType[rowsize][columnsize]; (or)
arrayType []arrayReferenceVariable[] = new arrayType[rowsize][columnsize];

How to initialize multidimensional array in Java

int[][] matrix = {{12, 13, 14}, {47, 34, 65}}; // This will create 2x3 multidimensional array
int[][] matrix = new int[][]{{12, 13, 14}, {47, 34, 65}}; // This will also create 2x3 multidimensional array

Example of multidimensional array in Java

class Example
{
	public static void main(String args[])
	{
		int x[][]={{1,2,3},{5,6,7},{8,9,10}};
		for(int a=0; a<3; a++)
		{
			for(int b=0; b<3; b++)
			{
					System.out.println(x[a][b]+" ");
			}
			System.out.println();
		}
	}
}

Output

1 2 3
5 6 7
8 9 10