DEV Community

Dev Cookies
Dev Cookies

Posted on

Arrays in Java: A Comprehensive Guide with All Operations & Methods

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]
Enter fullscreen mode Exit fullscreen mode

B. Direct Initialization

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

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]
Enter fullscreen mode Exit fullscreen mode

2. Array Operations

A. Accessing Elements

System.out.println(numbers[2]); // Output: 30
Enter fullscreen mode Exit fullscreen mode

B. Updating Elements

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

C. Finding Array Length

System.out.println(numbers.length); // Output: 5
Enter fullscreen mode Exit fullscreen mode

D. Iterating Over an Array

1. Using a for loop

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

2. Using an Enhanced for loop

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

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

Arrays.stream(numbers).forEach(System.out::println);
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

C. Copying an Array

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

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]
Enter fullscreen mode Exit fullscreen mode

E. Comparing Arrays (equals())

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

F. Converting an Array to a List

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

4. Multi-Dimensional Arrays

A. Declaring and Initializing

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

B. Accessing Elements

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

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();
}
Enter fullscreen mode Exit fullscreen mode

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};
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)