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 so as to create as well as access Java arrays dynamically. Arrays have got only static methods as well as methods of Object class.
Methods in Java Arrays with examples
The class Arrays which belongs to java. util package has got 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 making use of 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)
CompareUnsigned method would compare two arrays that are passed as parameters in a lexicographical style and treating them as unsigned. This method of Integer class would compare two integer values treating them as unsigned and then returning zero in case x is equal to 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)
CopyOfRange method would copy the mentioned range of the mentioned array 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)
DeepEquals method would return true in case the two mentioned arrays are deeply equal to the other array or not.
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
Method would return the hash code depending upon “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 so as to sort and search arrays, compare arrays, and the filling of 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 useful that an array is the collection of variables of the same data type.
Recommended Articles
This is a guide to the Array Methods in Java. Here we discuss the brief overview, methods in Java Arrays with examples and outputs in detail. You can also go through our other suggested articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses