Introduction
The Java Arrays class contains a static factory that allows to be viewed as . Following are the important points about Arrays −
-
This class contains various methods for manipulating arrays (such as sorting and searching).
-
The methods in this class throw a NullPointerException if the specified array reference is null.
Arrays Class Declaration
Following is the declaration for java.util.Arrays class −
public class Arrays extends Object
Arrays Class Methods
Sr.No. |
Method & Description |
---|---|
1 |
This method returns a fixed-size list backed by the specified array. |
2 |
This method searches the specified array of bytes for the specified value using the binary search algorithm. |
3 |
This method searches the specified array of chars for the specified value using the binary search algorithm. |
4 |
This method searches the specified array of doubles for the specified value using the binary search algorithm. |
5 |
This method searches the specified array of floats for the specified value using the binary search algorithm. |
6 |
This method searches the specified array of ints for the specified value using the binary search algorithm. |
7 |
This method searches a range of the specified array of longs for the specified value using the binary search algorithm. |
8 |
This method searches the specified array for the specified object using the binary search algorithm. |
9 |
This method searches the specified array of shorts for the specified value using the binary search algorithm. |
10 |
This method searches the specified array for the specified object using the binary search algorithm. |
11 |
This method compares two boolean arrays lexicographically. |
12 |
This method compares two byte arrays lexicographically. |
13 |
This method compares two char arrays lexicographically. |
14 |
This method compares two double arrays lexicographically. |
15 |
This method compares two float arrays lexicographically. |
16 |
This method compares two int arrays lexicographically. |
17 |
This method compares two long arrays lexicographically. |
18 |
This method compares two short arrays lexicographically. |
19 |
This method compares two byte arrays lexicographically, numerically treating elements as unsigned. |
20 |
This method compares two int arrays lexicographically, numerically treating elements as unsigned. |
21 |
This method compares two long arrays lexicographically, numerically treating elements as unsigned. |
22 |
This method compares two short arrays lexicographically, numerically treating elements as unsigned. |
23 |
This method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. |
24 |
This method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. |
25 |
This method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. |
26 |
This method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. |
27 |
This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
28 |
This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
29 |
This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
30 |
This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
31 |
This method copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. |
32 |
This method copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. |
33 |
This method copies the specified range of the specified array into a new array. |
34 |
This method copies the specified range of the specified array into a new array. |
35 |
This method copies the specified range of the specified array into a new array. |
36 |
This method copies the specified range of the specified array into a new array. |
37 |
This method copies the specified range of the specified array into a new array. |
38 |
This method copies the specified range of the specified array into a new array. |
39 |
This method copies the specified range of the specified array into a new array. |
40 |
This method copies the specified range of the specified array into a new array. |
41 |
This method copies the specified range of the specified array into a new array. |
42 |
This method returns true if the two specified arrays are deeply equal to one another. |
43 |
This method returns a hash code based on the “deep contents” of the specified array. |
44 |
This method returns a string representation of the “deep contents” of the specified array. |
45 |
This method returns true if the two specified arrays of booleans are equal to one another. |
46 |
This method returns true if the two specified arrays of bytes are equal to one another. |
47 |
This method returns true if the two specified arrays of chars are equal to one another. |
48 |
This method returns true if the two specified arrays of double are equal to one another. |
49 |
This method returns true if the two specified arrays of floats are equal to one another. |
50 |
This method returns true if the two specified arrays of ints are equal to one another. |
51 |
This method returns true if the two specified arrays of longs are equal to one another. |
52 |
This method returns true if the two specified arrays of shorts are equal to one another. |
53 |
This method returns true if the two specified arrays of Objects are equal to one another. |
54 |
This method returns true if the two specified arrays of Objects are equal to one another. |
55 |
This method assigns the specified boolean value to each element of the specified array of booleans. |
56 |
This method assigns the specified byte value to each element of the specified array of bytes. |
57 |
This method assigns the specified char value to each element of the specified array of chars. |
58 |
This method assigns the specified double value to each element of the specified array of doubles. |
59 |
This method assigns the specified float value to each element of the specified array of floats. |
60 |
This method assigns the specified int value to each element of the specified array of ints. |
61 |
This method assigns the specified long value to each element of the specified array of longs. |
62 |
This method assigns the specified Object reference to each element of the specified array of Objects. |
63 |
This method assigns the specified short value to each element of the specified array of shorts. |
64 |
This method returns a hash code based on the contents of the specified array. |
65 |
This method finds and returns the index of the first mismatch between two boolean arrays, otherwise return -1 if no mismatch is found. |
66 |
This method finds and returns the index of the first mismatch between two byte arrays, otherwise return -1 if no mismatch is found. |
67 |
This method finds and returns the index of the first mismatch between two char arrays, otherwise return -1 if no mismatch is found. |
68 |
This method finds and returns the index of the first mismatch between two double arrays, otherwise return -1 if no mismatch is found. |
69 |
This method finds and returns the index of the first mismatch between two int arrays, otherwise return -1 if no mismatch is found. |
70 |
This method finds and returns the index of the first mismatch between two long arrays, otherwise return -1 if no mismatch is found. |
71 |
This method finds and returns the index of the first mismatch between two short arrays, otherwise return -1 if no mismatch is found. |
72 |
This method finds and returns the index of the first mismatch between two object arrays, otherwise return -1 if no mismatch is found. |
73 |
This method finds and returns the index of the first mismatch between two object arrays, otherwise return -1 if no mismatch is found. |
74 |
This method cumulates, in parallel, each element of the given array in place, using the supplied function. |
75 |
This method cumulates, in parallel, each element of the given array in place, using the supplied function. |
76 |
This method cumulates, in parallel, each element of the given array in place, using the supplied function. |
77 |
This method cumulates, in parallel, each element of the given array in place, using the supplied function. |
78 |
This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. |
79 |
This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. |
80 |
This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. |
81 |
This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. |
82 |
This method sorts the specified array of bytes into ascending numerical order. |
83 |
This method sorts the specified array of chars into ascending numerical order. |
84 |
This method sorts the specified array of doubles into ascending numerical order. |
85 |
This method sorts the specified array of floats into ascending numerical order. |
86 |
This method sorts the specified array of ints into ascending numerical order. |
87 |
This method sorts the specified array of longs into ascending numerical order. |
88 |
This method sorts the specified array of shorts into ascending numerical order. |
89 |
This method sorts the specified array of objects into ascending order, according to the natural ordering of its elements. |
90 |
This method sorts the specified array of objects into ascending order, according to the ordering provided by the Comparator instance. |
91 |
This method sets all elements of the specified array, using the provided generator function to compute each element. |
92 |
This method sets all elements of the specified array, using the provided generator function to compute each element. |
93 |
This method sets all elements of the specified array, using the provided generator function to compute each element. |
94 |
This method sets all elements of the specified array, using the provided generator function to compute each element. |
95 |
This method returns a Spliterator.OfDouble covering all of the specified array. |
96 |
This method returns a Spliterator.OfInt covering all of the specified array. |
97 |
This method returns a Spliterator.OfLong covering all of the specified array. |
98 |
This method returns a Spliterator covering all of the specified array. |
99 |
This method returns a sequential DoubleStream with the specified array as its source. |
100 |
This method returns a sequential IntStream with the specified array as its source. |
101 |
This method returns a sequential LongStream with the specified array as its source. |
102 |
This method returns a sequential Stream with the specified array as its source. |
103 |
This method sorts the specified array of bytes into ascending numerical order. |
104 |
This method sorts the specified array of chars into ascending numerical order. |
105 |
This method sorts the specified array of doubles into ascending numerical order. |
106 |
This method sorts the specified array of floats into ascending numerical order. |
107 |
This method sorts the specified array of ints into ascending numerical order. |
108 |
This method sorts the specified array of longs into ascending numerical order. |
109 |
This method sorts the specified array of objects into ascending order, according to the natural ordering of its elements. |
110 |
This method sorts the specified array of shorts into ascending numerical order. |
111 |
This method returns a string representation of the contents of the specified array of boolean. |
Methods Inherited
This class inherits methods from the following classes −
-
java.util.Object
Arrays Class Example
This example demonstrates the binarySearch() method of Arrays class.
// Importing the Arrays class import java.util.Arrays; // Public Main Class public class Main { public static void main(String[] args) { // Declaring an array int arr[] = {8, 5, 3, 10, 2, 1, 15, 20}; // Sorting the array Arrays.sort(arr); // Taking an element to search int ele = 15; // Using binarySearch() method to search "ele" System.out.println( ele + " presents at the index = " + Arrays.binarySearch(arr, ele)); } }
Let us compile and run the above program, this will produce the following result −
15 presents at the index = 6