Types of array:

  • Single-Dimensional Array
  • Multi-Dimensional Array

One-Dimensional Array :

  • A one-dimensional array is used to store multiple values of the same data type in a single row.
  • It uses a single index to access the elements.

Syntax:

dataType[] arrayName = {value1, value2, value3}; 

Example:

public class OneD_Array { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; System.out.println(numbers[0]); System.out.println(numbers[1]); System.out.println(numbers[2]); } } 

Output:

10 20 30 

2. Multidimensional Array :

  • A multidimensional array is an array that contains another array as its elements.
  • It is used to store data in multiple rows and columns.
  • It is also called an array of arrays.
  • Elements in the two dimensional array can be accessed with two indexes: row index and column index.

Syntax:

dataType[][] arrayName = { {value1, value2}, {value3, value4} }; 

Example:

public class TwoD_Array { public static void main(String[] args) { int[][] a= { {10,20,30}, {11,22,33} }; System.out.println(a[0][2]); System.out.println(a[1][1]); } } 

Output:

30 22 

Finding the Number of Rows and Columns :

  • arrayName.length returns the number of rows.
  • arrayName[rowIndex].length returns the number of columns in that particular row.

Example:

public class TwoD_Length { public static void main(String[] args) { int[][] a= { {10,20,30,40}, {11,22,33,44}, {111,222,333,333} }; System.out.println("Number of rows : "+a.length); System.out.println("Number of Columns :"+a[0].length); } } 

Output:

Number of rows : 3 Number of Columns :4