Java Arrays Class – Methods, Examples & Common Usage

Package: java.util
public class Arrays extends Object

Important:

  • Arrays is a utility class
  • All methods are static
  • Used for sorting, searching, comparing, copying, filling, and converting arrays

1️⃣ Sorting Methods (Most Used)

static void sort(array)
Use: Sorts array in ascending order

int[] nums = {5, 3, 8, 1};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums)); // [1, 3, 5, 8]

static void sort(array, fromIndex, toIndex)
Use: Sort part of array

Arrays.sort(nums, 1, 3); // sorts index 1 to 2

static void parallelSort(array)
Use: Faster sorting for large arrays (multi‑threaded)

2️⃣ Searching Methods

static int binarySearch(array, key)
Use: Searches element using binary search
✅ Array must be sorted first

int index = Arrays.binarySearch(nums, 5);
System.out.println(index); // index of 5

static int binarySearch(array, fromIndex, toIndex, key)
Use: Search within a range

3️⃣ Comparison Methods

static boolean equals(array1, array2)
Use: Compare 1‑D arrays

int[] a = {1,2,3};
int[] b = {1,2,3};
System.out.println(Arrays.equals(a, b)); // true

static boolean deepEquals(array1, array2)
Use: Compare multi‑dimensional arrays

int[][] x = {{1,2},{3,4}};
int[][] y = {{1,2},{3,4}};
System.out.println(Arrays.deepEquals(x, y)); // true

static int compare(array1, array2)
Use: Lexicographical comparison

static int mismatch(array1, array2)
Use: Returns first index where arrays differ

int idx = Arrays.mismatch(new int[]{1,2,3}, new int[]{1,9,3});
System.out.println(idx); // 1

4️⃣ Filling Methods

static void fill(array, value)
Use: Initialize array with same value

int[] arr = new int[5];
Arrays.fill(arr, 10);

static void fill(array, fromIndex, toIndex, value)
Use: Partial fill

5️⃣ Copying Methods

static array copyOf(original, newLength)
Use: Resize or clone array

int[] copy = Arrays.copyOf(arr, 3);

static array copyOfRange(original, from, to)
Use: Extract portion of array

int[] sub = Arrays.copyOfRange(arr, 1, 4);

6️⃣ Conversion Methods

static String toString(array)
Use: Convert array to readable string

System.out.println(Arrays.deepToString(x));

static String deepToString(array)
Use: Multi‑dimensional arrays

System.out.println(Arrays.deepToString(x));

static List asList(T… a)
Use: Convert array to fixed‑size list
⚠️ Note: With primitive arrays, it returns a single element list (array reference).

String[] names = {"A","B","C"};
List<String> list = Arrays.asList(names);

7️⃣ Stream & Utility Methods (Java 8+)

static IntStream stream(int[] array)
Use: Functional operations

int sum = Arrays.stream(nums).sum();

static void setAll(array, generator)
Use: Populate array dynamically

✅ MOST COMMONLY USED Arrays METHODS (Interview + Projects)

🔥 Used very frequently

sort()
binarySearch()
equals()
toString()
fill()
copyOf()
copyOfRange()

🔶 Used in advanced scenarios

deepEquals()
deepToString()
parallelSort()
mismatch()
stream()

 

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment