Updated May 25, 2023
Introduction to Array Methods in Java
The Arrays class belongs to Java. The util package belongs to the Java Collection Framework. Array class gives methods that are static to create as well as access Java arrays dynamically. Arrays have only static methods and methods of the Object class.
Methods in Java Arrays with Examples
The class Arrays belongs to java.util package has numerous static methods that are useful in filling, sorting, searching, and many other things in arrays.
They are as follows:
1. static <T> List<T> asList (T… a)
asList method is used to return the fixed-size list that mentioned Arrays back.
Code:
// Program to showcase asList() method
import java.util.Arrays;
public class Array {
public static void main(String[] args)
{
// Fetching Array
int Arr[] = { 10, 30, 35, 52, 75 };
// Converting elements into list
System.out.println("The Integer Array as a List = "+ Arrays.asList(Arr));
}
}
Output:
2. static int binarySearch(itemToSearch)
This method would search for a mentioned element in the array through the Binary Search algorithm.
Code:
// Program to showcase binarySearch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Fetching Array
int Arr[] = { 10, 30, 35, 52, 75 };
Arrays.sort(Arr);
int ele = 35;
System.out.println (ele + " is found at index = "
+ Arrays.binarySearch(Arr, ele));
}
}
Output:
3. static <T> int binarySearch(T[] an int fromIndex, int toIndex, T key, Comparator<T> c)
This method would search the range of mentioned array for a specified object using a binary search algorithm.
Code:
// Program to showcase binarySearch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Fetching Array
int Arr[] = { 10, 30, 35, 52, 75 };
Arrays.sort(Arr);
int ele = 35;
System.out.println ( ele
+ " is found at index = "
+ Arrays.binarySearch(Arr, 1, 3, ele));
}
}
Output:
4. compareUnsigned(arr 1, arr 2)
The compareUnsigned method would compare two arrays passed as parameters in a lexicographical style and treat them as unsigned. This method of Integer class would compare two integer values treating them as unsigned and then returning zero in case x equals y.
Code:
// Program to showcase compareUnsigned() method
import java.lang.Integer;
class Arrays {
public static void main(String args[])
{
int m = 10;
int n = 20;
// as 10 less than 20, the output would be a value less than zero
System.out.println(Integer.compareUnsigned(m, n));
int x = 8;
int y = 8;
// as 8 equals 8, Output would be zero
System.out.println(Integer.compareUnsigned(x, y));
int e = 25;
int f = 8;
// as 25 is greater than 8, Output would be a value greater than zero
System.out.println(Integer.compareUnsigned(e, f));
int o = 15;
int p = -7;
// as 15 is greater than -7 but -7 would be treated as an unsigned number
// which will be greater than 15
// Output would be a value less than zero
System.out.println(Integer.compareUnsigned(o, p));
}
}
Output:
5. copyOf(original array, new length)
Copy method copies the mentioned array, truncates it, or pads it with a default value, but only if necessary so that copy has got the mentioned length.
Code:
// Java program to showcase
// Arrays.copyOf() method
import java.util.Arrays;
public class Example {
public static void main(String[] args)
{
// Fetching Array
int Arr[] = { 10, 25, 55, 22, 35};
// Printing the elements in a single line
System.out.println("The Integer Array is: "
+ Arrays.toString(Arr));
System.out.println("\nThe new Arrays fetched by copyOf is :\n");
System.out.println("Integer Array is: "
+ Arrays.toString(Arrays.copyOf(Arr, 10)));
}
}
Output:
6. copyOfRange(the previous array, startIndex, finishIndex)
The copyOfRange method would copy the mentioned array’s range into a new Array.
Code:
// Java program to showcase
// Arrays.copyOf() method
import java.util.Arrays;
public class Array{
public static void main(String[] args)
{
// Fetching Array
int Arr[] = {20, 30, 15, 22, 35 };
// Printing the elements in a single line
System.out.println("Integer Array is: "
+ Arrays.toString(Arr));
System.out.println("\nThe new Arrays through copyOfRange is :\n");
System.out.println("Integer Array: "
+ Arrays.toString(Arrays.copyOfRange(Arr, 1, 3)));
}
}
Output:
7. static boolean deepEquals(Object[] m1, Object[] m2)
The DeepEquals method would return true if the two mentioned arrays are deeply equal to the other array.
Code:
// Java program to showcase
// method Arrays.deepEquals()
import java.util.Arrays;
public class Array{
public static void main(String[] args)
{
// Fetching first Array
int Arr[][] = { {10, 20, 35, 82, 95} };
// Fetching second Array
int Arr2[][] = { { 10, 15, 22 } };
// Comparing both arrays
System.out.println("Arrays when compared: "
+ Arrays.deepEquals(Arr, Arr2));
}
}
Output:
8. static int deepHashCode(Object[] a): deepHashCode
The method would return the hash code depending upon the “deep contents” of the mentioned arrays.
Code:
// Java program to showcase
// Arrays.deepHashCode() method
import java.util.Arrays;
public class Array {
public static void main(String[] args)
{
// Fetching first Array
int Arr[][] = { { 10, 20, 15, 22, 35} };
// Getting deep hashCode of arrays
System.out.println(Arrays.deepHashCode(Arr));
}
}
Output:
Conclusion
Thus we can conclude that the java.util.Arrays class can contain numerous static methods to sort and search arrays, compare arrays, and fill array elements. All of these methods are overloaded for all of the primitive types. Also, an array is used in storing data; however, it is helpful that an array is the collection of variables of the same data type.
Recommended Articles
This is a guide to Array Methods in Java. Here we discuss the brief overview and methods in Java arrays with examples. You can also go through our other suggested articles to learn more –