For easier work with arrays, we can use a standard class Arrays from java.util package. This helper class contains some static methods that perform useful operations on arrays.
Here are some of the most used methods:
toString(type[] array)
Returns a string representation of the array. In this form, the values of all elements of the array are inside the square brackets in the order of their positions in the array and are mutually separated by commas.
import java.util.Arrays; class Test { public static void main(String[] args) { String[] names = {"John", "Steve", "Melissa", "Maria"}; System.out.println(Arrays.toString(names)); } }
Output: [John, Steve, Melissa, Maria]
copyOf(type [] a, int n)
Returns a new copy of the array, which consists of the first n of its elements.
import java.util.Arrays; class Test { public static void main(String[] args) { String[] names = {"John", "Steve", "Melissa", "Maria"}; String[] newNames = Arrays.copyOf(names, 2); System.out.println(Arrays.toString(newNames)); } }
Output: [John, Steve]
copyOfRange(type [] a, int i, int j)
A new copy of the array is returned from index i to index j.
import java.util.Arrays; class Test { public static void main(String[] args) { String[] names = {"John", "Steve", "Rayan", "Melissa", "Maria", "Ana"}; String[] newNames = Arrays.copyOfRange(names, 2, 4); System.out.println(Arrays.toString(newNames)); } }
Output: [Rayan, Melissa]
sort(type [] a)
Sorts elements in ascending order. It can be used for both numbers and strings.
import java.util.Arrays; class Test { public static void main(String[] args) { String[] strings = {"John", "Steve", "Rayan", "Melissa", "Maria", "Ana"}; int[] numbers = {4, 9, 1, 7, 3, 2}; Arrays.sort(strings); Arrays.sort(numbers); System.out.println(Arrays.toString(strings)); System.out.println(Arrays.toString(numbers)); } }
Output: [Ana, John, Maria, Melissa, Rayan, Steve] [1, 2, 3, 4, 7, 9]
binarySearch(type [] a, type v)
Searches for a specific element in the array using the binary search algorithm.
If the value of v is found in the array, the index of the corresponding element is returned. Otherwise, the negative value ???? is returned so that −???? – 1 corresponds to the position where the given value should be in the sorted array.
If the value of v is found in the array, the index of the corresponding element is returned. Otherwise, the negative value ???? is returned so that −???? – 1 corresponds to the position where the given value should be in the sorted array.
import java.util.Arrays; class Test { public static void main(String[] args) { int[] arrays = {4, 9, 1, 7, 3, 2}; System.out.println(Arrays.binarySearch(arrays, 1)); System.out.println(Arrays.binarySearch(arrays, 12)); } }
Output: 2 -7
fill(type [] a, type c)
Assign the value of the variable c to all array elements.
import java.util.Arrays; class Test { public static void main(String[] args) { int[] array = {4, 9, 1, 7, 3, 2}; Arrays.fill(array, 1); System.out.println(Arrays.toString(array)); } }
Output: [1, 1, 1, 1, 1, 1]
equals(type [] a, type [] b)
Returns true if the arrays a and b have the same length and the same corresponding elements. Otherwise, the value false is returned.
import java.util.Arrays; class Test { public static void main(String[] args) { int[] array1 = {4, 9, 1, 7, 3, 2}; int[] array2 = {4, 9, 1, 7, 3, 2}; int[] array3 = {1, 7, 3, 2}; int[] array4 = {4, 9, 1, 7, 3, 2}; System.out.println(Arrays.equals(array1, array2)); System.out.println(Arrays.equals(array3, array4)); } }
Output: true false
That was all about Arrays class in Java. Continue to the next lesson.
Happy Learning!