Java Arrays

In Java, arrays are used to store multiple values of the same data type. Here's an overview:

Array Declaration:

  • Arrays are declared using square brackets [].
  • Syntax: dataType[] arrayName; or dataType arrayName[];.

Array Initialization:

  • Arrays can be initialized using an array initializer {} or by specifying the size.
  • Syntax: dataType[] arrayName = {value1, value2, ...}; or dataType[] arrayName = new dataType[size];.

Accessing Array Elements:

  • Array elements are accessed using index numbers starting from 0.
  • Syntax: arrayName[index].

Array Length:

  • The length of an array can be obtained using the length property.
  • Syntax: arrayName.length.

Single-Dimensional Arrays:

  • Single-dimensional arrays store elements in a linear sequence.
  • Example: int[] numbers = {1, 2, 3, 4, 5};

Multidimensional Arrays:

  • Multidimensional arrays store elements in multiple dimensions (rows and columns).
  • Example: int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Array of Arrays (Jagged Arrays):

  • Jagged arrays are arrays of arrays with varying lengths.
  • Example: int[][] jaggedArray = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};

Array Methods:

  • The java.util.Arrays class provides various methods for working with arrays, such as sorting, searching, and filling.

Example 1

public class ArraysExample {
    public static void main(String[] args) {
        // Array declaration and initialization
        int[] numbers1 = {1, 2, 3, 4, 5};
        int[] numbers2 = new int[3]; // Array with size 3

        // Accessing array elements
        System.out.println("First element of numbers1: " + numbers1[0]);

        // Modifying array elements
        numbers2[0] = 10;
        numbers2[1] = 20;
        numbers2[2] = 30;

        // Array length
        System.out.println("Length of numbers1: " + numbers1.length);
        System.out.println("Length of numbers2: " + numbers2.length);

        // Multidimensional array
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        // Accessing elements of a multidimensional array
        System.out.println("Element at row 2, column 3: " + matrix[1][2]);

        // Using array methods
        int[] copy = java.util.Arrays.copyOf(numbers1, numbers1.length);
        java.util.Arrays.sort(numbers1);
        int index = java.util.Arrays.binarySearch(numbers1, 3);
    }
}

Example 2

public class ArraysExample {
    public static void main(String[] args) {
        // Single-Dimensional Array
        int[] numbers = {1, 2, 3, 4, 5};

        // Multidimensional Array
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        // Jagged Array
        int[][] jaggedArray = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};

        // Accessing elements of a single-dimensional array
        System.out.println("First element of numbers: " + numbers[0]);

        // Accessing elements of a multidimensional array
        System.out.println("Element at row 2, column 3: " + matrix[1][2]);

        // Accessing elements of a jagged array
        System.out.println("Element at row 3, column 2: " + jaggedArray[2][1]);
    }
}

 

Summary

Java arrays can be single-dimensional, multidimensional, or jagged arrays. Understanding the type of array is crucial for effectively storing and manipulating collections of data in Java programs.