Industry Expert Insight
As an experienced programmer, I have observed that in most cases, developers rely on Java's built-in utilities for handling data structures unless specific performance or design requirements demand custom implementations. The java.util package is particularly important as it provides a wide range of utility classes that simplify data handling. My recommendation is that every Java programmer should explore and master this package.
This page focuses on two key topics: performing different operations on arrays using the Arrays class, and working with dynamic arrays using the ArrayList class, which is a widely used subclass of the List interface from the java.util package. In my experience, ArrayList is extensively used in real-world applications, making it essential for your success in Java programming.
Class Arrays
The Arrays class in java.util provides methods to manipulate arrays such as sorting, searching, and filling.
import java.util.Arrays;
public class ArrayFillExample {
public static void main(String[] args) {
int[] numbers = {5, 3, 8, 1};
System.out.println("Original array: " + Arrays.toString(numbers));
// Fill array with a specific value
Arrays.fill(numbers, 0);
System.out.println("After fill: " + Arrays.toString(numbers));
}
}
Dynamic Change of Array Size
Arrays are fixed in size, but you can create a new larger array using Arrays.copyOf().
import java.util.Arrays;
class DynamicArray {
public static void main(String[] args) {
int[] oldArr = {1, 2, 3};
int[] newArr = Arrays.copyOf(oldArr, 5);
newArr[3] = 4; newArr[4] = 5;
System.out.println(Arrays.toString(newArr));
}
}
Arrays.copyOf() or use ArrayList for flexible
collections.Sorting of Arrays
Sorting arrays is essential for efficient data handling. The Arrays.sort() method provides an easy way to sort.
import java.util.Arrays;
public class SortExample {
public static void main(String[] args) {
int[] numbers = {12, 5, 7, 1, 9};
Arrays.sort(numbers);
System.out.println("Sorted array: " + Arrays.toString(numbers));
}
}
Arrays.copyOf() or use ArrayList for flexible
collections.Search for Values in Arrays
The Arrays.binarySearch() method allows efficient search in sorted arrays.
import java.util.Arrays;
public class SearchExample {
public static void main(String[] args) {
int[] numbers = {1, 3, 5, 7, 9};
int index = Arrays.binarySearch(numbers, 5);
System.out.println("Index of 5: " + index);
}
}
What is ArrayList and Its Usage
The ArrayList class is a resizable array implementation of the List interface. It allows dynamic growth, unlike standard arrays, and provides convenient methods for adding, removing, and accessing elements.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
System.out.println("Fruits list: " + fruits);
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
System.out.println("First fruit: " + fruits.get(0));
}
}