Arrays in Java are a way to store multiple values of the same type in a single variable. Java arrays are zero-indexed and have a fixed size, meaning once an array is created, its size cannot be changed.

1. Creating Arrays

1.1 Declaring and Initializing Arrays

You can declare and initialize arrays in different ways:

1.1.1 Declaration and Initialization

  public class ArrayExample {
    public static void main(String[] args) {
        // Declaration and initialization
        int[] numbers = {1, 2, 3, 4, 5};
        String[] names = {"Alice", "Bob", "Charlie"};
        
        // Accessing elements
        System.out.println("First number: " + numbers[0]);
        System.out.println("Second name: " + names[1]);
    }
}
  

1.1.2 Declaration and Later Initialization

  public class ArrayExample {
    public static void main(String[] args) {
        // Declaration
        int[] numbers;
        String[] names;
        
        // Initialization
        numbers = new int[]{1, 2, 3, 4, 5};
        names = new String[]{"Alice", "Bob", "Charlie"};
        
        // Accessing elements
        System.out.println("First number: " + numbers[0]);
        System.out.println("Second name: " + names[1]);
    }
}
  

2. Accessing Array Elements

Array elements are accessed using their index, starting from 0.

Code Example:

  public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        System.out.println("Element at index 2: " + numbers[2]); // Output: 30
    }
}
  

3. Looping Through Arrays

You can use loops to iterate through array elements.

3.1 Using a For Loop

  public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Element at index " + i + ": " + numbers[i]);
        }
    }
}
  

3.2 Using an Enhanced For Loop

  public class ArrayExample {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Charlie"};
        
        for (String name : names) {
            System.out.println("Name: " + name);
        }
    }
}
  

4. Multidimensional Arrays

Java supports multidimensional arrays, such as 2D arrays.

4.1 Declaration and Initialization

  public class ArrayExample {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        System.out.println("Element at [1][2]: " + matrix[1][2]); // Output: 6
    }
}
  

4.2 Looping Through Multidimensional Arrays

  public class ArrayExample {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
  

5. Common Array Operations

5.1 Finding the Length of an Array

Use the length property to get the size of the array.

  public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        System.out.println("Array length: " + numbers.length);
    }
}
  

5.2 Copying Arrays

Use System.arraycopy() to copy arrays.

  public class ArrayExample {
    public static void main(String[] args) {
        int[] original = {1, 2, 3, 4, 5};
        int[] copy = new int[original.length];
        
        System.arraycopy(original, 0, copy, 0, original.length);
        
        System.out.println("Copied array:");
        for (int num : copy) {
            System.out.print(num + " ");
        }
    }
}
  

5.3 Sorting Arrays

Use Arrays.sort() to sort arrays.

  import java.util.Arrays;

public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 8, 1, 2};
        Arrays.sort(numbers);
        
        System.out.println("Sorted array:");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
    }
}
  

6. Arrays of Objects

You can also create arrays of objects, such as String or custom classes.

Code Example:

  public class ArrayExample {
    public static void main(String[] args) {
        String[] names = new String[3];
        names[0] = "Alice";
        names[1] = "Bob";
        names[2] = "Charlie";
        
        for (String name : names) {
            System.out.println("Name: " + name);
        }
    }
}