Array in Java

An array is a data structure that allows to store a collection of elements with similar data type under a single variable name.

Here's how you can create and work with arrays in Java:

Array Declaration

To declare an array, you specify the data type of the elements followed by square brackets [], and then provide a name for the array variable. For example:

int[] numbers;  


Array Initialization

After declaring an array, you can initialize it with values using the new keyword. There are multiple ways to initialize an array:

  • Inline Initialization
    int[] numbers = { 40, 27, 50, 73, 83, 20, 90 };
    String[] fruits = {"Apple", "Mango", "Grapes", "Orange", "Papaya"};
  • Explicit Initialization
  • int[] numbers = new int[10];  
    numbers[0] = 10;  
    numbers[1] = 28;
    numbers[2] = 40;
    numbers[3] = 98;


Accessing Array Elements

The elements in array are accessed by its numerical index. The index numbering begins with 0. The length of an array is determined while creating an object of array. Once an array is created, the array size cannot be changed.

Example:

In the given code snippet, we have an array named numbers declared and initialized with several integer values. The value of the corresponding element is accessed by specifying the index within the square brackets. The index represents the position of the element in the array, starting from 0.

public class Sample {

   public static void main(String[] args) {

      int[] numbers = { 40, 27, 50, 73, 83, 20, 90 };

      System.out.println(numbers[0]);
      System.out.println(numbers[1]);
      System.out.println(numbers[2]);
      System.out.println(numbers[3]);
      System.out.println(numbers[4]);
      System.out.println(numbers[5]);
      System.out.println(numbers[6]);

   }

}

Output:

40
27
50
73
83
20
90


Accessing Array Elements using Loops

In a real-world programming situation, you would probably use one of these supported looping constructs (for, while, and do-while) to access each element of the array.

Example:

public class Sample {

   public static void main(String[] args) {

      int[] numbers = { 40, 27, 50, 73, 83, 20, 90 };

      for (int i = 0; i < numbers.length; i++) {
         System.out.println(numbers[i]);
      }
   }

}

Output:

40
27
50
73
83
20
90


Getting Array Length

The built-in length property can be used to determine the size of array.

Example:

public class Sample {

   public static void main(String[] args) {

      int[] numbers = { 40, 27, 50, 73, 83, 20, 90 };

      int lengthOfArray = numbers.length;
      System.out.println(lengthOfArray);
   }

}

Output:

7


Multidimensional Array

A multidimensional array is an array of arrays. A multidimensional array can be declared by using two or more sets of square brackets, such as String [][] names. The elements are accessed by a corresponding number of index values.

Example:

Here's an example that declares and initializes a 2D array of strings named names. The names array has two rows and three columns. The outermost braces {} indicate the initialization of the 2D array, and the inner braces {} represent the individual arrays within the 2D array.

public class Sample {

   public static void main(String[] args) {

      String[][] names = { { "Mr. ", "Mrs. ", "Ms. " }, { "A", "B", "C" } };

      System.out.println(names[0][0] + names[1][0]);
		
      System.out.println(names[0][1] + names[1][1]);

      System.out.println(names[0][2] + names[1][2]);
   }
}

Output:

Mr. A
Mrs. B
Ms. C


Here's another example, where we declare and initialize a 2D array called grid with three rows and three columns. We then access and print specific elements from the array using the syntax arrayName[rowIndex][columnIndex].

public class Example {

   public static void main(String[] args) {

   // Declare and initialize a 2D array
   int[][] grid = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
      };

    // Accessing and printing values from the 2D array
    System.out.println("Element at grid[0][0]: " + grid[0][0]); // Output: 1
    System.out.println("Element at grid[1][2]: " + grid[1][2]); // Output: 6
    System.out.println("Element at grid[2][1]: " + grid[2][1]); // Output: 8

    // Modifying a value in the 2D array
    grid[1][1] = 0;
    System.out.println("Modified element at grid[1][1]: " + grid[1][1]); // Output: 0
   }

}

Output:

Element at grid[0][0]: 1
Element at grid[1][2]: 6
Element at grid[2][1]: 8
Modified element at grid[1][1]: 0