Java Array Methods
We often need to sort, copy, compare, or print an array after creating it. java.util.Arrays provides helpers for these jobs. The examples below show which operations change the original array and which produce another result.
Java provides the java.util.Arrays class, which contains several useful methods for working with arrays. These methods make common operations such as sorting, searching, comparing, copying, and filling arrays much easier.
1. What is the Arrays Class in Java?
The Arrays class is a utility class available in the java.util package.
It provides static methods that can be used to perform different operations on arrays.
To use the Arrays class, import it using:
import java.util.Arrays;For example:
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {50, 20, 40, 10, 30};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}Output
[10, 20, 30, 40, 50]Here:
Arrays.sort()sorts the array.Arrays.toString()converts the array into a readable string representation.
2. Important Methods of the Arrays Class
Some commonly used methods are:
| Method | Purpose |
|---|---|
Arrays.toString() | Converts a one-dimensional array into a readable string |
Arrays.deepToString() | Converts a multi-dimensional array into a readable string |
Arrays.sort() | Sorts an array |
Arrays.parallelSort() | Sorts an array using parallel processing |
Arrays.binarySearch() | Searches for an element in a sorted array |
Arrays.equals() | Compares two one-dimensional arrays |
Arrays.deepEquals() | Compares multi-dimensional arrays |
Arrays.fill() | Fills an array with a specified value |
Arrays.copyOf() | Creates a copy of an array with a specified length |
Arrays.copyOfRange() | Copies a specific range of an array |
Arrays.mismatch() | Finds the first index where two arrays differ |
Arrays.compare() | Compares two arrays lexicographically |
Let's understand these methods with examples.
3. Arrays.toString()
The Arrays.toString() method is used to convert a one-dimensional array into a readable string.
Without Arrays.toString(), directly printing an array does not display its elements.
Example
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers);
}
}The output will look similar to:
[I@6d06d69cThis is not the actual array content.
Instead, use Arrays.toString().
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(numbers));
}
}Output
[10, 20, 30, 40, 50]Syntax
Arrays.toString(array);4. Arrays.deepToString()
The Arrays.deepToString() method is useful for displaying multi-dimensional arrays.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println(Arrays.deepToString(numbers));
}
}Output
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]Why use deepToString()?
For a two-dimensional or higher-dimensional array, Arrays.toString() is not appropriate for displaying all nested elements.
Use:
Arrays.deepToString(array);5. Arrays.sort()
The Arrays.sort() method sorts the elements of an array in ascending order.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {50, 10, 40, 20, 30};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}Output
[10, 20, 30, 40, 50]The original array is modified.
Before sorting:
50 10 40 20 30After sorting:
10 20 30 40 50Sorting a String Array
Arrays.sort() can also sort strings.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
String[] names = {
"Rahul",
"Amit",
"Sneha",
"Priya"
};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}
}Output
[Amit, Priya, Rahul, Sneha]6. Sorting a Portion of an Array
You can also sort only a specific range of an array.
Syntax
Arrays.sort(array, fromIndex, toIndex);The fromIndex is inclusive, while toIndex is exclusive.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {50, 40, 30, 20, 10};
Arrays.sort(numbers, 1, 4);
System.out.println(Arrays.toString(numbers));
}
}The elements at indexes 1, 2, and 3 are sorted.
Output
[50, 20, 30, 40, 10]7. Arrays.binarySearch()
The Arrays.binarySearch() method searches for an element in an array.
However, the array should be sorted before using binary search.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(numbers, 30);
System.out.println("Index: " + index);
}
}Output
Index: 2The value 30 is located at index 2.
Searching for an Element That Does Not Exist
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(numbers, 35);
System.out.println("Index: " + index);
}
}If the element is not found, the method returns a negative value.
Therefore, it is common to check the result.
if (index >= 0) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found");
}Important
Before using binarySearch():
Arrays.sort(numbers);should generally be performed if the array is not already sorted.
8. Arrays.equals()
The Arrays.equals() method compares two one-dimensional arrays.
It returns:
trueif both arrays contain the same elements in the same order.falseotherwise.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] first = {10, 20, 30};
int[] second = {10, 20, 30};
boolean result = Arrays.equals(first, second);
System.out.println(result);
}
}Output
trueDifferent Order
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] first = {10, 20, 30};
int[] second = {30, 20, 10};
System.out.println(Arrays.equals(first, second));
}
}Output
falseThe values are the same, but their order is different.
9. Arrays.deepEquals()
Arrays.deepEquals() is used to compare multi-dimensional arrays.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[][] first = {
{10, 20},
{30, 40}
};
int[][] second = {
{10, 20},
{30, 40}
};
System.out.println(Arrays.deepEquals(first, second));
}
}Output
trueFor nested arrays, deepEquals() checks the contents of the nested arrays.
10. Arrays.fill()
The Arrays.fill() method fills all elements of an array with the same value.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = new int[5];
Arrays.fill(numbers, 100);
System.out.println(Arrays.toString(numbers));
}
}Output
[100, 100, 100, 100, 100]Filling a Specific Range
You can also fill only a particular portion of an array.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
Arrays.fill(numbers, 1, 4, 99);
System.out.println(Arrays.toString(numbers));
}
}Output
[10, 99, 99, 99, 50]Indexes 1 through 3 are replaced with 99.
11. Arrays.copyOf()
The Arrays.copyOf() method creates a new array containing the elements of an existing array.
Syntax
Arrays.copyOf(originalArray, newLength);Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] original = {10, 20, 30};
int[] copy = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copy));
}
}Output
[10, 20, 30]The returned array is a separate array.
12. Copying into a Larger Array
If the new length is larger than the original array, the remaining elements receive their default values.
For an int array, the default value is 0.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] original = {10, 20, 30};
int[] copy = Arrays.copyOf(original, 5);
System.out.println(Arrays.toString(copy));
}
}Output
[10, 20, 30, 0, 0]13. Copying into a Smaller Array
If the new length is smaller, only the required number of elements are copied.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copy));
}
}Output
[10, 20, 30]14. Arrays.copyOfRange()
The copyOfRange() method copies a specific portion of an array.
Syntax
Arrays.copyOfRange(array, from, to);The from index is inclusive and the to index is exclusive.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int[] result = Arrays.copyOfRange(numbers, 1, 4);
System.out.println(Arrays.toString(result));
}
}Output
[20, 30, 40]Indexes 1, 2, and 3 were copied.
15. Arrays.mismatch()
The Arrays.mismatch() method finds the first index where two arrays have different elements.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] first = {10, 20, 30, 40};
int[] second = {10, 20, 35, 40};
int index = Arrays.mismatch(first, second);
System.out.println("Mismatch index: " + index);
}
}Output
Mismatch index: 2The values at index 2 are:
first = 30
second = 35Therefore, the first mismatch occurs at index 2.
When Arrays Are Equal
If the arrays are equal, Arrays.mismatch() returns -1.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] first = {10, 20, 30};
int[] second = {10, 20, 30};
System.out.println(Arrays.mismatch(first, second));
}
}Output
-116. Arrays.compare()
The Arrays.compare() method compares two arrays lexicographically.
It returns:
- A negative value if the first array is smaller.
0if both arrays are equal.- A positive value if the first array is greater.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] first = {10, 20, 30};
int[] second = {10, 20, 30};
int result = Arrays.compare(first, second);
System.out.println(result);
}
}Output
0Another Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] first = {10, 20, 30};
int[] second = {10, 25, 30};
int result = Arrays.compare(first, second);
System.out.println(result);
}
}The comparison reaches the first different values:
20
25Since 20 is smaller than 25, the result is negative.
17. Arrays.parallelSort()
The parallelSort() method sorts an array using a parallel sorting implementation.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {50, 10, 40, 20, 30};
Arrays.parallelSort(numbers);
System.out.println(Arrays.toString(numbers));
}
}Output
[10, 20, 30, 40, 50]For everyday small arrays, the difference between sort() and parallelSort() may not be noticeable.
18. Array Methods vs Arrays Class Methods
An important point for beginners is that Java arrays themselves have a limited set of built-in capabilities.
Methods such as:
sort()
fill()
binarySearch()
copyOf()are not methods directly available on an array object.
They belong to the java.util.Arrays utility class.
For example:
Arrays.sort(numbers);Here:
Arraysis the class.sort()is the static method.numbersis passed as an argument.
19. Practical Example: Sorting Student Marks
Suppose we have marks of students and want to display them in ascending order.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] marks = {85, 72, 91, 68, 79};
System.out.println("Before sorting:");
System.out.println(Arrays.toString(marks));
Arrays.sort(marks);
System.out.println("After sorting:");
System.out.println(Arrays.toString(marks));
}
}Output
Before sorting:
[85, 72, 91, 68, 79]
After sorting:
[68, 72, 79, 85, 91]20. Practical Example: Searching Student Marks
We can sort the marks and then use binarySearch().
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] marks = {85, 72, 91, 68, 79};
Arrays.sort(marks);
int searchMark = 79;
int index = Arrays.binarySearch(marks, searchMark);
if (index >= 0) {
System.out.println(searchMark + " found at index " + index);
} else {
System.out.println(searchMark + " not found");
}
}
}Output
79 found at index 221. Practical Example: Copying an Array
Suppose an application needs another array containing the same values.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println("Original: " + Arrays.toString(original));
System.out.println("Copy: " + Arrays.toString(copy));
}
}Output
Original: [10, 20, 30, 40, 50]
Copy: [10, 20, 30, 40, 50]22. Practical Example: Filling an Array
Suppose all elements initially need to contain a default value.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] seats = new int[5];
Arrays.fill(seats, 1);
System.out.println(Arrays.toString(seats));
}
}Output
[1, 1, 1, 1, 1]23. Methods for Multi-Dimensional Arrays
For multi-dimensional arrays, methods such as deepToString() and deepEquals() are particularly useful.
Example
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[][] first = {
{10, 20},
{30, 40}
};
int[][] second = {
{10, 20},
{30, 40}
};
System.out.println(Arrays.deepToString(first));
System.out.println(Arrays.deepEquals(first, second));
}
}Output
[[10, 20], [30, 40]]
true24. Arrays.toString() vs Arrays.deepToString()
| Method | Used For |
|---|---|
Arrays.toString() | One-dimensional arrays |
Arrays.deepToString() | Multi-dimensional/nested arrays |
Example
int[] a = {10, 20, 30};
System.out.println(Arrays.toString(a));For a two-dimensional array:
int[][] b = {
{10, 20},
{30, 40}
};
System.out.println(Arrays.deepToString(b));25. Arrays.equals() vs Arrays.deepEquals()
| Method | Purpose |
|---|---|
Arrays.equals() | Compares one-dimensional arrays |
Arrays.deepEquals() | Compares nested/multi-dimensional arrays |
For example:
Arrays.equals(first, second);is suitable for one-dimensional arrays.
For nested arrays:
Arrays.deepEquals(first, second);is more appropriate.
26. Important Range Concept
Several Arrays methods use this format:
fromIndex
toIndexThe fromIndex is inclusive, while the toIndex is exclusive.
For example:
Arrays.sort(numbers, 1, 4);works with:
index 1
index 2
index 3It does not include index 4.
This same concept is used by methods such as:
Arrays.sort()
Arrays.fill()
Arrays.copyOfRange()27. Common Mistakes
Mistake 1: Forgetting the Import
If you write:
Arrays.sort(numbers);you normally need:
import java.util.Arrays;Mistake 2: Using binarySearch() on an Unsorted Array
Avoid:
int[] numbers = {50, 10, 30, 20, 40};
Arrays.binarySearch(numbers, 30);Instead:
Arrays.sort(numbers);
Arrays.binarySearch(numbers, 30);Mistake 3: Using Arrays.toString() for a 2D Array
For example:
int[][] numbers = {
{10, 20},
{30, 40}
};For readable output, use:
Arrays.deepToString(numbers);Mistake 4: Forgetting That sort() Modifies the Array
Consider:
int[] numbers = {30, 10, 20};
Arrays.sort(numbers);The original numbers array is now:
[10, 20, 30]If you need to preserve the original order, make a copy first.
int[] copy = Arrays.copyOf(numbers, numbers.length);
Arrays.sort(copy);28. Complete Example Using Multiple Array Methods
The following program demonstrates several methods together.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {50, 20, 40, 10, 30};
// Display the array
System.out.println("Original: " + Arrays.toString(numbers));
// Sort
Arrays.sort(numbers);
System.out.println("Sorted: " + Arrays.toString(numbers));
// Search
int index = Arrays.binarySearch(numbers, 30);
System.out.println("30 found at index: " + index);
// Copy
int[] copy = Arrays.copyOf(numbers, numbers.length);
System.out.println("Copy: " + Arrays.toString(copy));
// Compare
System.out.println("Arrays equal: " +
Arrays.equals(numbers, copy));
// Fill
Arrays.fill(copy, 100);
System.out.println("After fill: " +
Arrays.toString(copy));
}
}Output
Original: [50, 20, 40, 10, 30]
Sorted: [10, 20, 30, 40, 50]
30 found at index: 2
Copy: [10, 20, 30, 40, 50]
Arrays equal: true
After fill: [100, 100, 100, 100, 100]29. Quick Reference
| Method | Example | Purpose |
|---|---|---|
toString() | Arrays.toString(arr) | Display 1D array |
deepToString() | Arrays.deepToString(arr) | Display nested array |
sort() | Arrays.sort(arr) | Sort array |
parallelSort() | Arrays.parallelSort(arr) | Parallel sorting |
binarySearch() | Arrays.binarySearch(arr, 20) | Search sorted array |
equals() | Arrays.equals(a, b) | Compare 1D arrays |
deepEquals() | Arrays.deepEquals(a, b) | Compare nested arrays |
fill() | Arrays.fill(arr, 10) | Fill elements |
copyOf() | Arrays.copyOf(arr, 5) | Copy array |
copyOfRange() | Arrays.copyOfRange(arr, 1, 4) | Copy a range |
mismatch() | Arrays.mismatch(a, b) | Find first difference |
compare() | Arrays.compare(a, b) | Compare arrays |
30. Key Points to Remember
-
The
Arraysclass belongs to thejava.utilpackage. -
Import it using:
import java.util.Arrays; -
Arrays.sort()sorts an array. -
Arrays.binarySearch()searches a sorted array. -
Arrays.toString()is useful for displaying one-dimensional arrays. -
Arrays.deepToString()is useful for multi-dimensional arrays. -
Arrays.equals()compares one-dimensional arrays. -
Arrays.deepEquals()compares nested arrays. -
Arrays.fill()fills elements with a specified value. -
Arrays.copyOf()creates a copy with a specified length. -
Arrays.copyOfRange()copies a selected range. -
Range-based methods generally use an inclusive starting index and an exclusive ending index.
-
Arrays.mismatch()identifies the first differing position. -
Arrays.compare()compares arrays lexicographically. -
Most methods in the
Arraysclass are static and are called using the class name.
Conclusion
Java provides the java.util.Arrays class to simplify many common array operations. Instead of manually writing loops for sorting, copying, comparing, filling, and searching, developers can use the utility methods provided by this class.
The most commonly used methods are:
Arrays.toString()
Arrays.sort()
Arrays.binarySearch()
Arrays.equals()
Arrays.fill()
Arrays.copyOf()
Arrays.copyOfRange()
Arrays.deepToString()
Arrays.deepEquals()Understanding these methods makes array manipulation easier and helps write cleaner and more maintainable Java programs.
See you in next page....🙂