Essential Methods of the Java Arrays Class
Sorting Methods
Full Array Sorting with sort
The sort method in the Arrays class sorts an entire array. While its internal implementation is complex, we can illustrate the concept using a basic insertion sort algorithm.
public static void exchangeValues(int[] data, int index1, int index2) {
if (index1 == index2) {
return;
}
data[index1] = data[index1] ^ data[index2];
data[index2] = data[index1] ^ data[index2];
data[index1] = data[index1] ^ data[index2];
}
public static void insertionSort(int[] data) {
if (data == null || data.length == 0) {
System.out.println("Array cannot be sorted");
return;
}
for (int current = 1; current < data.length; current++) {
for (int compare = current; compare > 0; --compare) {
if (data[compare] < data[compare - 1]) {
exchangeValues(data, compare, compare - 1);
}
}
}
}
Partial Array Sorting with sort
The sort method also supports sorting a specific range within an array, defined by fromIndex (inclusive) and toIndex (exclusive).
public static void insertionSortRange(int[] data, int start, int end) {
if (data == null || data.length == 0) {
System.out.println("Array cannot be sorted");
return;
} else if (start > end) {
System.out.println("Start index exceeds end index");
return;
} else if ((start < 0 || start >= data.length) || (end < 0 || end > data.length)) {
System.out.println("Index out of bounds");
return;
}
for (int i = start + 1; i < end; ++i) {
for (int j = i; j > start; --j) {
if (data[j] < data[j - 1]) {
exchangeValues(data, j, j - 1);
}
}
}
}
Additional Sorting Features
- The
Arraysclass includes overloadedsortmethods for various datta types. parallelSortis available for parallel sorting, differing in implementation fromsort.- Range validation is handled internally using a dedicated method in the class.
Searching Methods
Binary Search with binarySearch
The binarySearch method performs an efficient search with O(log n) time complexity, using a binary search algorithm.
public static int binarySearch(int[] data, int target) {
int low = 0;
int high = data.length - 1;
int middle;
while (low <= high) {
middle = low + ((high - low) >>> 1);
if (data[middle] < target) {
low = middle + 1;
} else if (data[middle] > target) {
high = middle - 1;
} else {
return middle;
}
}
return -1;
}
Equality Checking Methods
Full Array Equality with equals
The equals method compares two arrays for equality, handling edge cases like null references.
public static boolean arraysEqual(int[] first, int[] second) {
if (first == second) {
return true;
}
if (first == null || second == null) {
return false;
}
if (first.length != second.length) {
return false;
}
for (int i = 0; i < first.length; ++i) {
if (first[i] != second[i]) {
return false;
}
}
return true;
}
Partial Equality Checking
Methods are also available for comparing specific ranges within arrays.
Copying Methods
Array Copying with copyOf
The copyOf method creates a new array of a specified length, copying elements from the original.
public static int[] copyArray(int[] source, int newSize) {
if (source == null) {
throw new RuntimeException("Source array is null");
} else if (newSize < 0) {
throw new RuntimeException("New size cannot be negative");
}
int[] copy = new int[newSize];
int limit = Math.min(source.length, newSize);
for (int i = 0; i < limit; ++i) {
copy[i] = source[i];
}
return copy;
}
Implementation Details
copyOfinternally usesSystem.arraycopy, a native method implemanted in C/C++.- Range-specific copying methods are also provided.
Filling Methods
Array Filling with fill
The fill method assigns a specified value to all element in an array, with variants for partial ranges.
// Example usage: Arrays.fill(array, value);
The implementation is straightforward, setting each element to the given value.