Java From Beginner To Advanced

0% completed

Previous
Next
Java Array Method Reference

In Java, the Arrays class provides a collection of static methods that facilitate various operations on arrays, such as sorting, searching, and converting arrays to strings. These pre-defined methods enhance the efficiency and readability of your code by allowing you to perform common array tasks without manually implementing the underlying logic.

Using Array Methods

To utilize the methods provided by the Arrays class, you need to import it from the java.util package. The general syntax for using these methods is:

Arrays.methodName(arguments);
  • methodName: The name of the method you want to use (e.g., sort, binarySearch, toString).

  • arguments: The array or relevant parameters the method requires.

Example 1: Sorting an Array

Below is an example that demonstrates how to sort an array of integers in ascending order using the Arrays.sort() method.

Java
Java

. . . .

Explanation:

  • Original Array: The array numbers is initialized with unsorted values.

  • Arrays.toString(numbers): Converts the array to a string for printing.

  • Arrays.sort(numbers): Sorts the array in ascending order.

Example 2: Searching in an Array

This example demonstrates how to search for a specific element in a sorted array using the Arrays.binarySearch() method.

Java
Java

. . . .

Explanation:

  • Sorted Array: The array numbers must be sorted before using binarySearch().

  • Arrays.binarySearch(numbers, key): Searches for the key in the array and returns its index if found.

Common Pre-defined Methods in the Arrays Class

The Arrays class offers a variety of methods to perform common operations on arrays. Below is a table summarizing some of the most frequently used methods:

MethodDescriptionExample Usage
sort(int[] a)Sorts the specified array of integers into ascending order.Arrays.sort(new int[]{3, 1, 2}) results in {1, 2, 3}
binarySearch(int[] a, int key)Searches the specified array for the specified value using binary search. The array must be sorted prior to making this call.Arrays.binarySearch(new int[]{1, 2, 3}, 2) returns 1
toString(int[] a)Returns a string representation of the specified array.Arrays.toString(new int[]{1, 2, 3}) returns "[1, 2, 3]"
fill(int[] a, int val)Assigns the specified value to each element of the array.Arrays.fill(new int[3], 7) results in {7, 7, 7}
copyOf(int[] original, int newLength)Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.Arrays.copyOf(new int[]{1, 2, 3}, 5) results in {1, 2, 3, 0, 0}
equals(int[] a, int[] a2)Returns true if the two specified arrays of integers are equal to one another.Arrays.equals(new int[]{1,2}, new int[]{1,2}) returns true
deepToString(Object[] a)Returns a string representation of the "deep contents" of the specified array.Arrays.deepToString(new Object[]{new int[]{1,2}, "Hello"}) returns "[[1, 2], Hello]"

The Arrays class in Java is a powerful tool that simplifies many common array operations. By leveraging these pre-defined methods, you can write more efficient, readable, and maintainable code. Familiarize yourself with the various methods available in the Arrays class to enhance your ability to manipulate and manage arrays effectively in your Java programs.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next