Java Arrays class consists exclusively of static methods that operates on array.
Java Arrays
- Java Arrays class was introduced in Java 1.2 as a utility class to perform various common operations on arrays in java.
- This class is part of java collections framework.
- Java Arrays class contains methods for sorting, searching and comparing arrays.
- Arrays class also contains a static method that returns a list backed by the specified array.
- Arrays class contains overloaded methods that support primitive data types. These methods are:
1234567public static <T> List<T> asList(T… a)public static void sort(int[] a)public static int binarySearch(int[] a, int k)public static boolean equals(int[] a, int[] a2)<img class="alignnone wp-image-30642 size-full" src="https://all-learning.com/wp-content/uploads/2017/11/Java-Arrays1.png" alt="Java-Arrays" width="1206" height="628" />
Java Arrays Examples
Let’s have a look at the Arrays methods example through some programs.
Java Arrays asList
Java Arrays provides asList method that returns a list backed by the specified array. Below is a simple program for showing array as a List.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.journaldev.examples; import java.util.Arrays; import java.util.List; /** * Java Arrays Example Program * * @author pankaj * */ public class ArraysAsListExample { public static void main(String[] args) { String[] strings = {"one", "two", "three", "four", "five"}; // strings array is converted into a List List<String> list = Arrays.asList(strings); System.out.println(list); } } |
Output of above program is:
1 2 3 |
<span style="color: #008000;"><strong><code> [one, two, three, four, five] </code></strong></span> |
Java Arrays sort
Java Arrays provides sort method that sorts the element of specified array and also sorts the specified range of the given array into ascending order.
Below is a simple program for sorting arrays in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.journaldev.examples; import java.util.Arrays; /** * Java Arrays Example Program * * @author pankaj * */ public class ArraysSortExample { public static void main(String[] args) { // Sorting Character char[] chars = {'B', 'D', 'C', 'A', 'E'}; Arrays.sort(chars); System.out.print("Sorted Characters : "); for (char character : chars) { System.out.print(character+" "); } // Sorting Integer int[] integers = {5, 2, 1, 4, 3}; Arrays.sort(integers); System.out.print("nSorted Integers : "); for (int i : integers) { System.out.print(i+" "); } // Sorting Specific Range of Integers int[] ints = {5, 2, 1, 4, 3, 9, 6, 8, 7, 10}; int fromIndex = 2; int toIndex = 7; Arrays.sort(ints, fromIndex, toIndex); System.out.print("nSorted Integers of Specific Range : "); for (int i : ints) { System.out.print(i+" "); } } } |
Output of above program is:
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> Sorted Characters : A B C D E Sorted Integers : 1 2 3 4 5 Sorted Integers of Specific Range : 5 2 1 3 4 6 9 8 7 10 </code></strong></span> |
Java Arrays binarySearch
Java Arrays binarySearch method uses binary search algorithm to search specified value from the elements of specified array and also searches from the specified range of the given array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.journaldev.examples; import java.util.Arrays; /** * Java Arrays binarySearch * * @author pankaj * */ public class ArraysBinarySearchExample { public static void main(String[] args) { // Searching a value from array of integer int[] integers = { 5, 2, 1, 4, 3, 9, 6, 8, 7, 10 }; int index = Arrays.binarySearch(integers, 2); if (index >= 0) { System.out.println("Element is found at the index :" + index); } else { System.out.println("Element is not found"); } // Searching a value from array of integer with specific range int fromIndex = 2; int toIndex = 7; int index2 = Arrays.binarySearch(integers, fromIndex, toIndex, 9); if (index2 >= 0) { System.out.println("Element is found at the index :" + index2); } else { System.out.println("Element is not found"); } } } |
Output of above program is:
1 2 3 4 |
<span style="color: #008000;"><strong><code> Element is found at the index :1 Element is found at the index :5 </code></strong></span> |
Java Arrays equals
Java Arrays provides equals method that is used to compare two arrays of the same type and returns boolean result. It returns true if two given arrays are equal to one another.
Below is a simple program to compare arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.journaldev.examples; import java.util.Arrays; /** * Java Arrays Example Program * * @author pankaj * */ public class ArraysEqualExample { public static void main(String[] args) { // Compare two arrays of type integer which are equal int[] a1 = { 1, 2, 3 }; int[] a2 = { 1, 2, 3 }; boolean equal = Arrays.equals(a1, a2); if (equal) { System.out.println("Arrays a1 and a2 are equal with Result : " + equal); }else { System.out.println("Arrays a1 and a2 are not equal with Result : " + equal); } // Compare two arrays of type integer which are not equal int[] b1 = { 1, 2, 3 }; int[] b2 = { 4, 5, 6 }; boolean equal1 = Arrays.equals(b1, b2); if (equal1) { System.out.println("Arrays b1 and b2 are equal with Result : " + equal1); }else { System.out.println("Arrays b1 and b2 are not equal with Result : " + equal1); } } } |
Output of above program is given below.
1 2 3 4 |
<span style="color: #008000;"><strong><code> Arrays a1 and a2 are equal with Result : true Arrays b1 and b2 are not equal with Result : false </code></strong></span> |
Compare Nested Arrays
What if the array is inside of another array? Will Arrays equal method is capable to compare nested arrays?
Let’s have a look at the below example program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.journaldev.examples; import java.util.Arrays; /** * Java Arrays Example Program * * @author pankaj * */ public class ArraysNestedArrayExample { public static void main(String[] args) { // Compare two nested arrays of type integer which are equal int[] a1 = { 1, 2, 3 }; int[] a2 = { 1, 2, 3 }; Object[] b1 = {a1}; Object[] b2 = {a2}; boolean equal = Arrays.equals(b1, b2); if (equal) { System.out.println("Arrays b1 and b2 are equal with Result : " + equal); } else { System.out.println("Arrays b1 and b2 are not equal with Result : " + equal); } } } |
Output of above program is:
1 2 3 |
<span style="color: #008000;"><strong><code> Arrays b1 and b2 are not equal with Result : false </code></strong></span> |
We can see that the output of the program is false despite the two arrays are equal and contains same number of elements.
Actually equal method is not able to compare nested arrays, for that Arrays have another method called deepEquals.
Let’s have a look at the below example program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.journaldev.examples; import java.util.Arrays; /** * Java Arrays Example Program * * @author pankaj * */ public class ArraysNestedArrayExample { public static void main(String[] args) { // Compare two nested arrays of type integer which are equal int[] a1 = { 1, 2, 3 }; int[] a2 = { 1, 2, 3 }; Object[] b1 = {a1}; Object[] b2 = {a2}; boolean equal = Arrays.deepEquals(b1, b2); if (equal) { System.out.println("Arrays b1 and b2 are equal with Result : " + equal); } else { System.out.println("Arrays b1 and b2 are not equal with Result : " + equal); } } } |
Output of above arrays equals program is:
1 2 3 |
<span style="color: #008000;"><strong><code> Arrays b1 and b2 are equal with Result : true </code></strong></span> |
That’s all for java Arrays class, I hope nothing important got missed here.
Reference: API Doc