java.util.Arrays Class in Java
java.util.Arrays Class
This class contains various methods for manipulating arrays (such as sorting and searching).
The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
Arrays Class Methods
public static int binarySearch(byte[] a,byte key)
Searches the specified array of bytes for the specified value using the binary search algorithm. The array must be sorted (as by the sort(byte[]) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
public static int binarySearch(byte[] a,int fromIndex,int toIndex,byte key)
Searches a range of the specified array of bytes for the specified value using the binary search algorithm. The range must be sorted (as by the sort(byte[], int, int) method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.
public static int binarySearch(int[] a, int key)
Searches the specified array of ints for the specified value using the binary search algorithm.
public static int binarySearch(int[] a,int fromIndex,int toIndex,int key)
Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by the sort(int[], int, int) method) prior to making this call
public static boolean[] copyOf(boolean[] original, int newLength)
public static byte[] copyOf(byte[] original, int newLength)
public static char[] copyOf(char[] original, int newLength)
public static double[] copyOf(double[] original, int newLength)
public static float[] copyOf(float[] original, int newLength)
public static int[] copyOf(int[] original, int newLength)
public static long[] copyOf(long[] original, int newLength)
public static short[] copyOf(short[] original, int newLength)
public static <T> T[] copyOf(T[] original, int newLength)
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
public static boolean equals(boolean[] a, boolean[] a2)
Returns true if the two specified arrays of booleans are equal to one another.
public static boolean equals(byte[] a, byte[] a2)
Returns true if the two specified arrays of bytes are equal to one another.
public static boolean equals(char[] a, char[] a2)
Returns true if the two specified arrays of chars are equal to one another.
public static boolean equals(double[] a, double[] a2)
Returns true if the two specified arrays of doubles are equal to one another.
public static boolean equals(float[] a, float[] a2)
Returns true if the two specified arrays of floats are equal to one another.
public static boolean equals(int[] a, int[] a2)
Returns true if the two specified arrays of ints are equal to one another.
public static boolean equals(long[] a, long[] a2)
Returns true if the two specified arrays of longs are equal to one another.
public static boolean equals(Object[] a, Object[] a2)
Returns true if the two specified arrays of Objects are equal to one another.
public static boolean equals(short[] a, short[] a2)
Returns true if the two specified arrays of shorts are equal to one another.
public static void fill(boolean[] a, boolean val)
Assigns the specified boolean value to each element of the specified array of booleans.
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
Assigns the specified boolean value to each element of the specified range of the specified array of booleans.
publicstatic void fill(byte[] a, byte val)
Assigns the specified byte value to each element of the specified array of bytes.
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
Assigns the specified byte value to each element of the specified range of the specified array of bytes.
public static void fill(char[] a, char val)
Assigns the specified char value to each element of the specified array of chars.
public static void fill(char[] a, int fromIndex, int toIndex, char val)
Assigns the specified char value to each element of the specified range of the specified array of chars.
public static void fill(double[] a, double val)
Assigns the specified double value to each element of the specified array of doubles.
public static void fill(double[] a, int fromIndex, int toIndex, double val)
Assigns the specified double value to each element of the specified range of the specified array of doubles.
public static void fill(float[] a, float val)
Assigns the specified float value to each element of the specified array of floats.
public static void fill(float[] a, int fromIndex, int toIndex, float val)
Assigns the specified float value to each element of the specified range of the specified array of floats.
public static void fill(int[] a, int val)
Assigns the specified int value to each element of the specified array of ints.
public static void fill(int[] a, int fromIndex, int toIndex, int val)
Assigns the specified int value to each element of the specified range of the specified array of ints.
public static void fill(long[] a, int fromIndex, int toIndex, long val)
Assigns the specified long value to each element of the specified range of the specified array of longs.
public static void fill(long[] a, long val)
Assigns the specified long value to each element of the specified array of longs.
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
Assigns the specified Object reference to each element of the specified range of the specified array of Objects.
public static void fill(Object[] a, Object val)
Assigns the specified Object reference to each element of the specified array of Objects.
public static void fill(short[] a, int fromIndex, int toIndex, short val)
Assigns the specified short value to each element of the specified range of the specified array of shorts.
public static void fill(short[] a, short val)
Assigns the specified short value to each element of the specified array of shorts.
public static int hashCode(boolean[] a)
public static int hashCode(byte[] a)
public static int hashCode(char[] a)
public static int hashCode(double[] a)
public static int hashCode(float[] a)
public static int hashCode(int[] a)
public static int hashCode(long[] a)
public static int hashCode(Object[] a)
public static int hashCode(short[] a)
Returns a hash code based on the contents of the specified array.
public static void sort(byte[] a)
public static void sort(char[] a)
public static void sort(double[] a)
public static void sort(float[] a)
public static void sort(int[] a)
public static void sort(long[] a)
public static void sort(short[] a)
Sorts the specified array into ascending numerical order.
public static void sort(Object[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
public static void sort(byte[] a, int fromIndex, int toIndex)
public static void sort(char[] a, int fromIndex, int toIndex)
public static void sort(double[] a, int fromIndex, int toIndex)
public static void sort(float[] a, int fromIndex, int toIndex)
public static void sort(int[] a, int fromIndex, int toIndex)
public static void sort(long[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
public static void sort(Object[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
public static void sort(short[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
public static <T> void sort(T[] a, Comparator<? super T> c)
Sorts the specified array of objects according to the order induced by the specified comparator.
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
Arrays Class Example
import java.util.Arrays;
public class ArraysExample {
public static void main(String[] args) {
// Example 1: Sorting arrays
int[] array1 = {5, 2, 9, 1, 5, 6};
System.out.println("Original array1: " + Arrays.toString(array1));
Arrays.sort(array1);
System.out.println("Sorted array1: " + Arrays.toString(array1));
// Example 2: Binary search
int[] array2 = {10, 20, 30, 40, 50};
int key = 30;
int index = Arrays.binarySearch(array2, key);
System.out.println("Index of " + key + " in array2: " + index);
// Example 3: Comparing arrays
int[] array3 = {1, 2, 3};
int[] array4 = {1, 2, 3};
int[] array5 = {3, 2, 1};
System.out.println("array3 equals to array4: " + Arrays.equals(array3, array4)); // true
System.out.println("array3 equals to array5: " + Arrays.equals(array3, array5)); // false
// Example 4: Filling arrays
char[] array6 = new char[5];
Arrays.fill(array6, '*');
System.out.println("Filled array6: " + Arrays.toString(array6));
// Example 5: Copying arrays
int[] array7 = {1, 2, 3, 4, 5};
int[] array8 = Arrays.copyOf(array7, 3); // Copy first 3 elements
int[] array9 = Arrays.copyOfRange(array7, 2, 4); // Copy elements from index 2 to 3 (exclusive)
System.out.println("Copied array8: " + Arrays.toString(array8));
System.out.println("Copied array9: " + Arrays.toString(array9));
// Example 6: Sorting arrays with custom comparator
String[] array10 = {"Java", "Python", "C", "JavaScript"};
Arrays.sort(array10, (s1, s2) -> s1.length() - s2.length());
System.out.println("Sorted array10 by length: " + Arrays.toString(array10));
// Example 7: Converting arrays to lists
Integer[] array11 = {1, 2, 3, 4, 5};
java.util.List<Integer> list = Arrays.asList(array11);
System.out.println("List from array11: " + list);
}
}
Output:
Original array1: [5, 2, 9, 1, 5, 6]
Sorted array1: [1, 2, 5, 5, 6, 9]
Index of 30 in array2: 2
array3 equals to array4: true
array3 equals to array5: false
Filled array6: [*, *, *, *, *]
Copied array8: [1, 2, 3]
Copied array9: [3, 4]
Sorted array10 by length: [C, Java, Python, JavaScript]
List from array11: [1, 2, 3, 4, 5]