HOME HTML EDITOR C JAVA PHP

Java Arrays Methods

In Java, an array is used to store multiple values in a single variable. Arrays are very important in programming because they help store and manage large amounts of data efficiently.

Java provides a special class called Arrays (in java.util package) that contains many useful methods to work with arrays. These methods help in sorting, searching, comparing, filling, and converting arrays.

1. Importing Arrays Class

To use array methods, you need to import the Arrays class:

import java.util.Arrays;

2. Arrays.toString()

The toString() method converts an array into a readable string format. It is mainly used to print arrays easily.

import java.util.Arrays; int[] numbers = {10, 20, 30, 40}; System.out.println(Arrays.toString(numbers));

Output: [10, 20, 30, 40]

3. Arrays.sort()

The sort() method is used to sort an array in ascending order.

import java.util.Arrays; int[] numbers = {5, 2, 8, 1}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers));

Output: [1, 2, 5, 8]

This method works for integers, doubles, characters, and strings.

4. Arrays.binarySearch()

The binarySearch() method searches for a specific value in a sorted array. If the value is found, it returns the index position.

import java.util.Arrays; int[] numbers = {1, 3, 5, 7, 9}; int index = Arrays.binarySearch(numbers, 5); System.out.println(index);

Output: 2

Important: The array must be sorted before using binarySearch().

5. Arrays.equals()

The equals() method compares two arrays. It returns true if both arrays contain the same elements in the same order.

import java.util.Arrays; int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; System.out.println(Arrays.equals(arr1, arr2));

Output: true

6. Arrays.fill()

The fill() method fills the entire array with a specific value.

import java.util.Arrays; int[] numbers = new int[5]; Arrays.fill(numbers, 100); System.out.println(Arrays.toString(numbers));

Output: [100, 100, 100, 100, 100]

7. Arrays.copyOf()

The copyOf() method copies an array into a new array of a specified length.

import java.util.Arrays; int[] original = {1, 2, 3}; int[] copy = Arrays.copyOf(original, 5); System.out.println(Arrays.toString(copy));

Output: [1, 2, 3, 0, 0]

If the new array is larger, remaining values are filled with default values.

8. Arrays.copyOfRange()

The copyOfRange() method copies a specific range from an array.

import java.util.Arrays; int[] numbers = {10, 20, 30, 40, 50}; int[] newArray = Arrays.copyOfRange(numbers, 1, 4); System.out.println(Arrays.toString(newArray));

Output: [20, 30, 40]

9. Arrays.deepToString()

The deepToString() method is used for multi-dimensional arrays.

import java.util.Arrays; int[][] matrix = {{1, 2}, {3, 4}}; System.out.println(Arrays.deepToString(matrix));

Output: [[1, 2], [3, 4]]

10. Arrays.parallelSort()

The parallelSort() method sorts large arrays faster using multiple threads.

import java.util.Arrays; int[] numbers = {9, 4, 7, 1}; Arrays.parallelSort(numbers); System.out.println(Arrays.toString(numbers));

Output: [1, 4, 7, 9]

Why Array Methods Are Important?

Difference Between Manual and Built-in Methods

Without built-in methods, you would need to write your own logic for sorting, searching, and copying arrays. This increases code length and complexity.

Using Arrays class methods makes programming easier and more reliable.

Conclusion

Java provides many powerful array methods through the Arrays class. These methods help in sorting, searching, comparing, copying, and printing arrays efficiently.

Every Java programmer should understand array methods because arrays are used in almost every application. Mastering these methods will improve your programming skills and help you write better code.

Tip: Always use built-in array methods instead of writing long manual logic. It makes your program faster and cleaner.