๐งฎ Java Arrays Methods โ Complete Guide with Syntax, Examples, and Best Practices (2025)
๐งฒ Introduction โ Why Learn Java Array Methods?
Arrays in Java are a fundamental data structure used to store multiple values in a single variable. However, working with raw arrays can be tedious unless you know how to manipulate them efficiently using array methods and utility functions.
With Java Array methods, you can:
- โ Search, sort, copy, and fill arrays quickly
- โ Improve your performance with optimized algorithms
- โ
Leverage
java.util.Arrays
for real-world tasks
๐ What Are Java Array Methods?
In Java, raw arrays are objects, but they donโt come with built-in methods. Instead, Java provides a utility class:
๐ java.util.Arrays
which offers a wide range of static helper methods for array manipulation.
๐ฆ Commonly Used Java Array Methods (via java.util.Arrays
)
๐ง Method | ๐ Purpose | โ Example |
---|---|---|
sort(array) | Sorts the array | Arrays.sort(arr) |
copyOf(array, length) | Copies the array to new length | Arrays.copyOf(arr, 5) |
copyOfRange(arr, start, end) | Partial copy | Arrays.copyOfRange(arr, 2, 5) |
equals(arr1, arr2) | Checks equality | Arrays.equals(arr1, arr2) |
fill(array, value) | Fills array with specified value | Arrays.fill(arr, 0) |
toString(array) | Converts array to string | Arrays.toString(arr) |
binarySearch(array, key) | Searches for a value | Arrays.binarySearch(arr, 5) |
setAll(array, lambda) | Sets each element using lambda | Arrays.setAll(arr, i -> i * 2) |
stream(array) | Converts to Stream | Arrays.stream(arr) |
โ Java Arrays Method Examples
โ
1. Arrays.sort()
โ Sort an Array
import java.util.Arrays;
int[] nums = {4, 1, 3, 9};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
Output:
[1, 3, 4, 9]
โ Explanation: Sorts array in ascending order.
โ
2. Arrays.copyOf()
โ Copy Array with New Length
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, 5);
System.out.println(Arrays.toString(copy));
Output:
[1, 2, 3, 0, 0]
โ Explanation: Pads extra elements with default values.
โ
3. Arrays.copyOfRange()
โ Copy a Range
int[] values = {10, 20, 30, 40, 50};
int[] sub = Arrays.copyOfRange(values, 1, 4);
System.out.println(Arrays.toString(sub));
Output:
[20, 30, 40]
โ Explanation: Copies elements from index 1 to 3 (end exclusive).
โ
4. Arrays.equals()
โ Compare Two Arrays
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(Arrays.equals(a, b)); // true
โ Explanation: Compares element-by-element (not just reference).
โ
5. Arrays.fill()
โ Set All Elements to One Value
int[] filled = new int[5];
Arrays.fill(filled, 7);
System.out.println(Arrays.toString(filled));
Output:
[7, 7, 7, 7, 7]
โ
Explanation: Fills every element with 7
.
โ
6. Arrays.toString()
โ Print Array
int[] data = {1, 2, 3};
System.out.println(Arrays.toString(data));
Output:
[1, 2, 3]
โ Explanation: Converts array into readable string.
โ
7. Arrays.binarySearch()
โ Fast Search (sorted arrays only)
int[] sorted = {1, 3, 5, 7};
int index = Arrays.binarySearch(sorted, 5);
System.out.println("Found at index: " + index);
Output:
Found at index: 2
โ Explanation: Performs binary search and returns index.
โ ๏ธ Note: Array must be sorted before using binarySearch()
.
โ
8. Arrays.setAll()
โ Use Lambda to Fill Array
int[] square = new int[5];
Arrays.setAll(square, i -> i * i);
System.out.println(Arrays.toString(square));
Output:
[0, 1, 4, 9, 16]
โ Explanation: Fills array using a formula for each index.
โ
9. Arrays.stream()
โ Convert to Stream for Functional Ops
int[] arr = {1, 2, 3, 4};
int sum = Arrays.stream(arr).sum();
System.out.println("Sum: " + sum);
Output:
Sum: 10
โ Explanation: Converts array to Stream API for advanced processing.
๐ Table Summary: Arrays Method Categories
๐ฆ Category | ๐ง Methods |
---|---|
Sorting | sort() |
Copying | copyOf() , copyOfRange() |
Searching | binarySearch() |
Filling | fill() , setAll() |
Comparing | equals() |
Converting | toString() , stream() |
๐ก Java Array Method Tips
- โ
Always sort before using
binarySearch()
. - โ
Use
Arrays.toString()
to print arrays (instead of loops). - โ
For dynamic array-like behavior, prefer
ArrayList
.
๐ง Summary โ Java Arrays Utility Methods
Mastering java.util.Arrays
methods allows you to:
- โ Perform fast array manipulations
- โ Avoid writing repetitive loops
- โ Leverage built-in optimized logic
From sorting and copying to advanced transformations with lambdas, these methods are vital for writing clean, efficient, and bug-free Java code.
โFAQs on Java Arrays Methods
โ Why canโt I use arr.toString()
directly?
It prints the object’s memory reference. Use Arrays.toString(arr)
for readable output.
โ What happens if I copy an array to a smaller length?
Only the specified number of elements are copied. Extra values are truncated.
โ Can I use binarySearch()
on an unsorted array?
No. The result is undefined unless the array is sorted.
โ Is Arrays.equals()
deep or shallow?
It performs a shallow comparison for 1D arrays. For 2D arrays, use Arrays.deepEquals()
.
โ How do I resize an array in Java?
Java arrays are fixed-size. Use Arrays.copyOf()
to simulate resizing.
Share Now :