An array in Java is a data structure that stores elements of the same type in a contiguous memory location. Arrays are fixed in size and provide fast element access using an index.

This blog will cover:

  1. Declaring & Initializing Arrays
  2. Array Operations
  3. Built-in Array Methods
  4. Multi-Dimensional Arrays
  5. Array Utilities (Arrays class)
  6. Performance Considerations

1. Declaring & Initializing Arrays

A. Declaration & Memory Allocation

// Declaration without initialization
int[] arr1;
String[] arr2;

// Declaration with size allocation
int[] arr3 = new int[5]; // Default values: [0, 0, 0, 0, 0]

B. Direct Initialization

int[] numbers = {10, 20, 30, 40, 50};
String[] names = {"Alice", "Bob", "Charlie"};

C. Using Loops for Initialization

int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
    arr[i] = i * 10;
}
System.out.println(Arrays.toString(arr)); // Output: [0, 10, 20, 30, 40]

2. Array Operations

A. Accessing Elements

System.out.println(numbers[2]); // Output: 30

B. Updating Elements

numbers[1] = 25;
System.out.println(Arrays.toString(numbers)); // Output: [10, 25, 30, 40, 50]

C. Finding Array Length

System.out.println(numbers.length); // Output: 5

D. Iterating Over an Array

1. Using a for loop

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

2. Using an Enhanced for loop

for (int num : numbers) {
    System.out.print(num + " ");
}

3. Using Arrays.stream() (Java 8+)

Arrays.stream(numbers).forEach(System.out::println);

3. Built-in Array Methods

A. Sorting an Array

int[] arr = {5, 2, 8, 1, 9};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // Output: [1, 2, 5, 8, 9]

B. Searching an Array (binarySearch())

Binary search works on sorted arrays.

int index = Arrays.binarySearch(arr, 8);
System.out.println("Index of 8: " + index); // Output: Index of 8: 3

C. Copying an Array

int[] copy = Arrays.copyOf(arr, arr.length);
System.out.println(Arrays.toString(copy)); // Output: [1, 2, 5, 8, 9]

D. Filling an Array (fill())

int[] arr = new int[5];
Arrays.fill(arr, 7);
System.out.println(Arrays.toString(arr)); // Output: [7, 7, 7, 7, 7]

E. Comparing Arrays (equals())

int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
System.out.println(Arrays.equals(arr1, arr2)); // Output: true

F. Converting an Array to a List

List<String> list = Arrays.asList("Java", "Python", "C++");
System.out.println(list); // Output: [Java, Python, C++]

4. Multi-Dimensional Arrays

A. Declaring and Initializing

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

B. Accessing Elements

System.out.println(matrix[1][2]); // Output: 6

C. Iterating Through a 2D Array

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();
}

D. Jagged Arrays (Irregular Row Sizes)

int[][] jagged = new int[3][];
jagged[0] = new int[]{1, 2};
jagged[1] = new int[]{3, 4, 5};
jagged[2] = new int[]{6};

5. Utility Methods from Arrays Class

Method Description
sort(arr) Sorts the array
binarySearch(arr, key) Searches for a key in a sorted array
copyOf(arr, newLength) Copies an array to a new length
fill(arr, val) Fills an array with a specific value
equals(arr1, arr2) Compares two arrays
toString(arr) Converts an array to a string
asList(arr) Converts an array to a list

Example:

System.out.println(Arrays.toString(numbers));

6. Performance Considerations

  • Fixed Size: Use ArrayList when the size is dynamic.
  • Efficient Read Access: O(1) time complexity.
  • Insertion/Deletion: Costly if resizing is needed.

When to Use ArrayList Instead?

Use ArrayList when:

  • The size is unknown or dynamic.
  • Frequent insertions and deletions are needed.
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
list.add(4);
System.out.println(list); // Output: [1, 2, 3, 4]

7. Conclusion

This blog covered all array operations, built-in methods, multi-dimensional arrays, and performance considerations. Arrays are useful for performance-critical applications but are fixed in size. For dynamic operations, consider using ArrayList.

Would you like to explore ArrayList vs. LinkedList in the next blog? Let me know in the comments!