๐Ÿ“š Java Reference
Estimated reading: 4 minutes 48 views

๐Ÿงฎ 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 arrayArrays.sort(arr)
copyOf(array, length)Copies the array to new lengthArrays.copyOf(arr, 5)
copyOfRange(arr, start, end)Partial copyArrays.copyOfRange(arr, 2, 5)
equals(arr1, arr2)Checks equalityArrays.equals(arr1, arr2)
fill(array, value)Fills array with specified valueArrays.fill(arr, 0)
toString(array)Converts array to stringArrays.toString(arr)
binarySearch(array, key)Searches for a valueArrays.binarySearch(arr, 5)
setAll(array, lambda)Sets each element using lambdaArrays.setAll(arr, i -> i * 2)
stream(array)Converts to StreamArrays.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
Sortingsort()
CopyingcopyOf(), copyOfRange()
SearchingbinarySearch()
Fillingfill(), setAll()
Comparingequals()
ConvertingtoString(), 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java Arrays Methods

Or Copy Link

CONTENTS
Scroll to Top