Java.util.Arrays Class API Guide
Java.util.Arrays Class API Guide
2. Searching an Array
•static int binarySearch(byte[] a, byte key) - Searches the specified array of bytes
for the specified value using the binary search algorithm.
•static int binarySearch(char[] a, char key) - Searches the specified array of chars
for the specified value using the binary search algorithm.
•static int binarySearch(double[] a, double key) - Searches the specified array of
doubles for the specified value using the binary search algorithm.
•static int binarySearch(float[] a, float key) - Searches the specified array of
floats for the specified value using the binary search algorithm.
•static int binarySearch(int[] a, int key) - Searches the specified array of ints for
the specified value using the binary search algorithm.
•static int binarySearch(long[] a, long key) - Searches the specified array of longs
for the specified value using the binary search algorithm.
•static int binarySearch(Object[] a, Object key) - Searches the specified array for
the specified object using the binary search algorithm.
Blog Site: Java Guides Author: Ramesh Fadatare
3. Copies an Array
•static int[] copyOf(int[] original, int newLength) - Copies the specified array,
truncating or padding with zeros (if necessary) so the copy has the specified length.
6. Sorting an Array
•static void sort(byte[] a) - Sorts the specified array into ascending numerical order.
•static void sort(byte[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(char[] a) - Sorts the specified array into ascending numerical order.
•static void sort(char[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(double[] a) - Sorts the specified array into ascending numerical
order.
•static void sort(double[] a, int fromIndex, int toIndex) - Sorts the specified
range of the array into ascending order.
•static void sort(float[] a) - Sorts the specified array into ascending numerical order.
•static void sort(float[] a, int fromIndex, int toIndex) - Sorts the specified
range of the array into ascending order.
•static void sort(int[] a) - Sorts the specified array into ascending numerical order.
•static void sort(int[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(long[] a) - Sorts the specified array into ascending numerical order.
•static void sort(long[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(Object[] a) - Sorts the specified array of objects into ascending order,
according to the natural ordering of its elements.
Blog Site: Java Guides Author: Ramesh Fadatare
•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.
•static void sort(short[] a) - Sorts the specified array into ascending numerical order.
•static void sort(short[] a, int fromIndex, int toIndex) - Sorts the specified
range of the array into ascending order.
•static void sort(T[] a, Comparator<? super T> c) - Sorts the specified array of
objects according to the order induced by the specified comparator.
•static 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.
import java.util.Arrays;
Blog Site: Java Guides Author: Ramesh Fadatare
import java.util.List;
/**
* This class shows different methods to convert Array to ArrayList
*
* @author javaguides.net
*
*/
public class ArrayToArrayList {
Output:
Original ArrayList from Arrays.asList()
Agra
Mysore
Blog Site: Java Guides Author: Ramesh Fadatare
Chandigarh
Bhopal
Let's see one more example, convert Integer Array to ArrayList of Integer type.
Output:
1
2
3
4
5
6
2. Searching an Array
Arrays class provides many overloaded search() methods to search the specified array for the
specified object using the binary search algorithm.
•static int binarySearch(byte[] a, byte key) - Searches the specified array of bytes
for the specified value using the binary search algorithm.
•static int binarySearch(char[] a, char key) - Searches the specified array of chars
for the specified value using the binary search algorithm.
•static int binarySearch(double[] a, double key) - Searches the specified array of
doubles for the specified value using the binary search algorithm.
•static int binarySearch(float[] a, float key) - Searches the specified array of
floats for the specified value using the binary search algorithm.
•static int binarySearch(int[] a, int key) - Searches the specified array of ints for
the specified value using the binary search algorithm.
Blog Site: Java Guides Author: Ramesh Fadatare
•static int binarySearch(long[] a, long key) - Searches the specified array of longs
for the specified value using the binary search algorithm.
•static int binarySearch(Object[] a, Object key) - Searches the specified array for
the specified object using the binary search algorithm.
•static int binarySearch(short[] a, short key) - Searches the specified array of
shorts for the specified value using the binary search algorithm.
•static int binarySearch(T[] a, T key, Comparator<? super T> c) - Searches the
specified array for the specified object using the binary search algorithm.
Example: This example demonstrates the usage of above all search methods.
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* java.util.Arrays Class API examples
*
* @author javaguides.net
*
*/
public class ArraysJavaUtilClass {
// Searches the specified array for the specified String using the
// binary search algorithm.
final String key = "abc";
String[] strArray = { "abc", "cdf", "pqr" };
int index = Arrays.binarySearch(strArray, key);
System.out.println(" String key found at index : " + index);
}
}
Blog Site: Java Guides Author: Ramesh Fadatare
3. Copying an Array
•static int[] copyOf(int[] original, int newLength) - Copies the specified array,
truncating or padding with zeros (if necessary) so the copy has the specified length.
If you want to copy first few elements of an array or full copy of the array, you can use this method.
Obviously, it’s not versatile like System.arraycopy() but it’s also not confusing and easy to use.
This method internally use System arraycopy() method.
import java.util.Arrays;
/**
* This class shows different methods for copy array in java
* @author javaguides.net
*
*/
public class JavaArrayCopyExample {
System.out.println(
"Copy First five elements of array. Result array
= " + Arrays.toString(dest));
}
}
Output:
Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
Two Arrays Deep Equals :: true
}
}
Output:
fillArray (before): [0, 0, 0, 0, 0]
fillArray (after): [1, 1, 1, 1, 1]
6. Sorting an Array
•static void sort(byte[] a) - Sorts the specified array into ascending numerical order.
•static void sort(byte[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(char[] a) - Sorts the specified array into ascending numerical order.
Blog Site: Java Guides Author: Ramesh Fadatare
•static void sort(char[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(double[] a) - Sorts the specified array into ascending numerical
order.
•static void sort(double[] a, int fromIndex, int toIndex) - Sorts the specified
range of the array into ascending order.
•static void sort(float[] a) - Sorts the specified array into ascending numerical order.
•static void sort(float[] a, int fromIndex, int toIndex) - Sorts the specified
range of the array into ascending order.
•static void sort(int[] a) - Sorts the specified array into ascending numerical order.
•static void sort(int[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(long[] a) - Sorts the specified array into ascending numerical order.
•static void sort(long[] a, int fromIndex, int toIndex) - Sorts the specified range
of the array into ascending order.
•static void sort(Object[] a) - Sorts the specified array of objects into ascending order,
according to the natural ordering of its elements.
•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.
•static void sort(short[] a) - Sorts the specified array into ascending numerical order.
•static void sort(short[] a, int fromIndex, int toIndex) - Sorts the specified
range of the array into ascending order.
•static void sort(T[] a, Comparator<? super T> c) - Sorts the specified array of
objects according to the order induced by the specified comparator.
•static 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 provides many sort() overloaded methods and below is the example to demonstrate a
few of these methods.
Example:
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* java.util.Arrays Class API examples
*
Blog Site: Java Guides Author: Ramesh Fadatare
* @author javaguides.net
*
*/
public class ArraysJavaUtilClass {
Output:
Original Array : [3, 1, 2, 4]
Sorted Array : [1, 2, 3, 4]
Original Array : [1, 3, 2, 1, 5]
Sorted Array : [1, 1, 2, 3, 5]
Original Array : [a, d, c, b]
Sorted Array : [a, b, c, d]
Original Array : [0.1, 0.3, 0.2]
Sorted Array : [0.1, 0.2, 0.3]
Original Array : [1, 3, 2, 5, 4]
Sorted Array : [1, 2, 3, 4, 5]
Original Array : [1.1, 1.5, 1.4]
Sorted Array : [1.1, 1.4, 1.5]
Output:
Blog Site: Java Guides Author: Ramesh Fadatare
Output:
Original Array : [2018-08-13, 2017-12-12]
Sorted Array : [2017-12-12, 2018-08-13]
/**
* java.util.Arrays Class API examples
*
Blog Site: Java Guides Author: Ramesh Fadatare
* @author javaguides.net
*
*/
public class ArraysJavaUtilClass {
}
}
Output:
int Array toString : [3, 1, 2, 4]
byte Array toString : [1, 3, 2, 1, 5]
char Array toString : [a, d, c, b]
double Array toString : [0.1, 0.3, 0.2]
long Array toString : [1, 3, 2, 5, 4]
float Array toString : [1.1, 1.5, 1.4]
string Array toString : [abc, cdf, pqr]