Arrays class in Java Java by Rajesh Kumar Sahanee - May 19, 2019April 14, 20200 Post Views: 5,537 Hello Friends, today I am going to share a program on Arrays class in Java to demonstrate uses of Arrays Class. The Arrays class is defined in java.util package is part of Java Collection Framework. Arrays class provides static methods to dynamically create and access Java arrays. This class contains various methods for manipulating arrays (such as sorting and searching). So Here is the program code ArraysClassTest.java ArraysClassTest.java Java import java.util.Arrays; import java.util.Comparator; class ArraysClassTest { public static void main(String args[]) { //asList method converts any type of array to List int intA1[] = {1, 2, 3, 4, 5}; String stringA1[] = {"one", "two", "three", "four"}; System.out.println("Integer array to list: " + Arrays.asList(intA1)); System.out.println("String array to list: " + Arrays.asList(stringA1)); //binarySearch method is used to search in array using binary search algorithm int intA2[] = {1, 2, 3, 4, 5, 6, 7, 8}; System.out.println("3 is found at index: " + Arrays.binarySearch(intA2, 3)); //index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). System.out.println("3 is found at index: " + Arrays.binarySearch(intA2, 0, 2, 3)); //compare method Compares two Object arrays, within comparable elements, lexicographically. int intA3[] = {1, 2, 3, 4, 5}; int intA4[] = {1, 3, 5}; System.out.println("compare function Result: " + Arrays.compare(intA3, intA4)); //compareUnsigned Compares two int arrays lexicographically, numerically treating elements as unsigned. System.out.println("compareUnsigned Result: " + Arrays.compareUnsigned(intA3, intA4)); //copyOf copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length String stringA2[] = {"one", "two", "three", "four", "five"}; String stringA2Copy[] = Arrays.copyOf(stringA2, 10); System.out.println("stringA2 Copy: " + Arrays.asList(stringA2Copy)); //copyOfRange method copies the specified range of the specified array into a new array. String stringA2Copy2[] = Arrays.copyOfRange(stringA2, 0, 3); System.out.println("stringA2 Copy using copyOfRange method: " + Arrays.asList(stringA2Copy2)); //deepEquals returns true if the two specified arrays are deeply equal to one another. int intA5[][] = {{1, 2, 3, 4, 5}}; int intA6[][] = {{1, 2, 3}}; System.out.println("deepEquals Result: " + Arrays.deepEquals(intA5, intA6)); //deepHashCode returns a hash code based on the "deep contents" of the specified array. int intA7[][] = {{1, 2, 3, 4, 5}, {1, 2, 3}}; System.out.println("deepHashCode Result: " + Arrays.deepHashCode(intA7)); //deepToString method returns a string representation of the "deep contents" of the specified array. int intA8[][] = {{1, 2, 3, 4, 5}, {1, 2, 3}}; System.out.println("deepToString Result: " + Arrays.deepToString(intA8)); //equals method returns true if the two specified arrays of Objects are equal to one another int intA9[] = {1, 2, 3, 4, 5}; int intA10[] = {1, 2, 3, 4, 5}; System.out.println("equals method Result: " + Arrays.equals(intA9, intA10)); //fill method assigns the specified Object reference to each element of the specified array of Objects. String stringA3[] = {"one", "two", "three", "four", "five"}; Arrays.fill(stringA3, "one"); System.out.println("fill method Result: " + Arrays.asList(stringA3)); //hashCode returns a hash code based on the content of the specified array. int intA11[] = {1, 2, 3, 4, 5}; System.out.println("hashCode Result: " + Arrays.hashCode(intA11)); //mismatch method finds and returns the index of the first mismatch between two Object arrays, otherwise return -1 if no mismatch. int intA12[] = {1, 2, 3, 4, 5}; int intA13[] = {1, 2, 3, 5, 5}; System.out.println("intA12 and intA13 mismatched at index: " + Arrays.mismatch(intA12, intA13)); //parallelPrefix method cumulates, in parallel, each element of the given array in place, using the supplied function. String stringA4[] = {"a", "b", "c", "d", "e"}; Arrays.parallelPrefix(stringA4, (x, y) -> x + y); System.out.println("parallelPrefix method result: " + Arrays.asList(stringA4)); //parallelPrefix method cumulates, in parallel, specified range of elements of the given array in place, using the supplied function. String stringA5[] = {"a", "b", "c", "d", "e"}; Arrays.parallelPrefix(stringA5, 1, 4, (x, y) -> x + y); System.out.println("parallelPrefix method with range result: " + Arrays.asList(stringA5)); //parallelSetAll method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. int intA14[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; java.util.function.IntUnaryOperator generator = index -> { return intA14[index] * 2; }; Arrays.parallelSetAll(intA14, generator); System.out.print("parallelSetAll method result: "); for (int e : intA14) { System.out.print(e + " "); } System.out.println(); //parallelSort sorts the specified array of objects into ascending order, according to the natural ordering of its elements String stringA6[] = {"a", "c", "b", "e", "d"}; Arrays.parallelSort(stringA6); System.out.println("[a, c, b, e, d] after parallelSort: " + Arrays.asList(stringA6)); //setAll method sets all elements of the specified array, using the provided generator function to compute each element. String stringA7[] = new String[5]; Arrays.setAll(stringA7, (index) -> "a" + index); System.out.println("setAll method result: " + Arrays.asList(stringA7)); //sort method sorts the specified array of objects into ascending order, according to the natural ordering of its elements String stringA8[] = {"d", "a", "e", "b", "a", "b", "c"}; Arrays.sort(stringA8); System.out.println("sort method resullt: " + Arrays.asList(stringA8)); //sort method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. String stringA9[] = {"d", "a", "e", "b", "a", "b", "c"}; Arrays.sort(stringA9, 1, 5); System.out.println("sort method with specified range resullt: " + Arrays.asList(stringA9)); //sort method sorts the specified array of objects into ascending order, according to the natural ordering of its elements Student studentA1[] = {new Student(3, "Student3"), new Student(4, "Student4"), new Student(2, "Student2"), new Student(1, "Student1")}; System.out.println("studentA1 array before sort: " + Arrays.asList(studentA1)); Arrays.sort(studentA1, new SortByRoll()); System.out.println("studentA1 array after sort: " + Arrays.asList(studentA1)); //sort method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. Student studentA2[] = {new Student(3, "Student3"), new Student(4, "Student4"), new Student(2, "Student2"), new Student(1, "Student1")}; System.out.println("studentA2 array before sort: " + Arrays.asList(studentA2)); Arrays.sort(studentA2, 1, 4, new SortByRoll()); System.out.println("studentA2 array after sort: " + Arrays.asList(studentA2)); //spliterator method returns a Spliterator covering all of the specified array. String stringA10[] = {"a", "b", "c", "d"}; System.out.print("spliterator result: "); Arrays.spliterator(stringA10).forEachRemaining(d -> System.out.print(d + " ")); System.out.println(); //spliterator method returns a Spliterator covering the specified range of the specified array. String stringA11[] = {"a", "b", "c", "d"}; System.out.print("spliterator with range result: "); Arrays.spliterator(stringA11, 1, 3).forEachRemaining(d -> System.out.print(d + " ")); System.out.println(); //stream method returns a sequential Stream with the specified array as its source. int intA15[] = {1, 2, 3, 4, 5}; System.out.print("stream with foreach result: "); Arrays.stream(intA15).forEach(e -> System.out.print(e + " ")); System.out.println(); //toString returns a string representation of the contents of the specified array. int intA16[] = {1, 2, 3, 4, 5}; System.out.println("toString method result: " + Arrays.toString(intA16)); }//end main }//end Class class Student { int roll; String name; public Student(int roll, String name) { this.roll = roll; this.name = name; } @Override public String toString() { return roll + " - " + name; } } class SortByRoll implements Comparator<Student> { @Override public int compare(Student a, Student b) { return a.roll - b.roll; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 import java.util.Arrays;import java.util.Comparator; class ArraysClassTest { public static void main(String args[]) { //asList method converts any type of array to List int intA1[] = {1, 2, 3, 4, 5}; String stringA1[] = {"one", "two", "three", "four"}; System.out.println("Integer array to list: " + Arrays.asList(intA1)); System.out.println("String array to list: " + Arrays.asList(stringA1)); //binarySearch method is used to search in array using binary search algorithm int intA2[] = {1, 2, 3, 4, 5, 6, 7, 8}; System.out.println("3 is found at index: " + Arrays.binarySearch(intA2, 3)); //index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). System.out.println("3 is found at index: " + Arrays.binarySearch(intA2, 0, 2, 3)); //compare method Compares two Object arrays, within comparable elements, lexicographically. int intA3[] = {1, 2, 3, 4, 5}; int intA4[] = {1, 3, 5}; System.out.println("compare function Result: " + Arrays.compare(intA3, intA4)); //compareUnsigned Compares two int arrays lexicographically, numerically treating elements as unsigned. System.out.println("compareUnsigned Result: " + Arrays.compareUnsigned(intA3, intA4)); //copyOf copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length String stringA2[] = {"one", "two", "three", "four", "five"}; String stringA2Copy[] = Arrays.copyOf(stringA2, 10); System.out.println("stringA2 Copy: " + Arrays.asList(stringA2Copy)); //copyOfRange method copies the specified range of the specified array into a new array. String stringA2Copy2[] = Arrays.copyOfRange(stringA2, 0, 3); System.out.println("stringA2 Copy using copyOfRange method: " + Arrays.asList(stringA2Copy2)); //deepEquals returns true if the two specified arrays are deeply equal to one another. int intA5[][] = {{1, 2, 3, 4, 5}}; int intA6[][] = {{1, 2, 3}}; System.out.println("deepEquals Result: " + Arrays.deepEquals(intA5, intA6)); //deepHashCode returns a hash code based on the "deep contents" of the specified array. int intA7[][] = {{1, 2, 3, 4, 5}, {1, 2, 3}}; System.out.println("deepHashCode Result: " + Arrays.deepHashCode(intA7)); //deepToString method returns a string representation of the "deep contents" of the specified array. int intA8[][] = {{1, 2, 3, 4, 5}, {1, 2, 3}}; System.out.println("deepToString Result: " + Arrays.deepToString(intA8)); //equals method returns true if the two specified arrays of Objects are equal to one another int intA9[] = {1, 2, 3, 4, 5}; int intA10[] = {1, 2, 3, 4, 5}; System.out.println("equals method Result: " + Arrays.equals(intA9, intA10)); //fill method assigns the specified Object reference to each element of the specified array of Objects. String stringA3[] = {"one", "two", "three", "four", "five"}; Arrays.fill(stringA3, "one"); System.out.println("fill method Result: " + Arrays.asList(stringA3)); //hashCode returns a hash code based on the content of the specified array. int intA11[] = {1, 2, 3, 4, 5}; System.out.println("hashCode Result: " + Arrays.hashCode(intA11)); //mismatch method finds and returns the index of the first mismatch between two Object arrays, otherwise return -1 if no mismatch. int intA12[] = {1, 2, 3, 4, 5}; int intA13[] = {1, 2, 3, 5, 5}; System.out.println("intA12 and intA13 mismatched at index: " + Arrays.mismatch(intA12, intA13)); //parallelPrefix method cumulates, in parallel, each element of the given array in place, using the supplied function. String stringA4[] = {"a", "b", "c", "d", "e"}; Arrays.parallelPrefix(stringA4, (x, y) -> x + y); System.out.println("parallelPrefix method result: " + Arrays.asList(stringA4)); //parallelPrefix method cumulates, in parallel, specified range of elements of the given array in place, using the supplied function. String stringA5[] = {"a", "b", "c", "d", "e"}; Arrays.parallelPrefix(stringA5, 1, 4, (x, y) -> x + y); System.out.println("parallelPrefix method with range result: " + Arrays.asList(stringA5)); //parallelSetAll method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. int intA14[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; java.util.function.IntUnaryOperator generator = index -> { return intA14[index] * 2; }; Arrays.parallelSetAll(intA14, generator); System.out.print("parallelSetAll method result: "); for (int e : intA14) { System.out.print(e + " "); } System.out.println(); //parallelSort sorts the specified array of objects into ascending order, according to the natural ordering of its elements String stringA6[] = {"a", "c", "b", "e", "d"}; Arrays.parallelSort(stringA6); System.out.println("[a, c, b, e, d] after parallelSort: " + Arrays.asList(stringA6)); //setAll method sets all elements of the specified array, using the provided generator function to compute each element. String stringA7[] = new String[5]; Arrays.setAll(stringA7, (index) -> "a" + index); System.out.println("setAll method result: " + Arrays.asList(stringA7)); //sort method sorts the specified array of objects into ascending order, according to the natural ordering of its elements String stringA8[] = {"d", "a", "e", "b", "a", "b", "c"}; Arrays.sort(stringA8); System.out.println("sort method resullt: " + Arrays.asList(stringA8)); //sort method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. String stringA9[] = {"d", "a", "e", "b", "a", "b", "c"}; Arrays.sort(stringA9, 1, 5); System.out.println("sort method with specified range resullt: " + Arrays.asList(stringA9)); //sort method sorts the specified array of objects into ascending order, according to the natural ordering of its elements Student studentA1[] = {new Student(3, "Student3"), new Student(4, "Student4"), new Student(2, "Student2"), new Student(1, "Student1")}; System.out.println("studentA1 array before sort: " + Arrays.asList(studentA1)); Arrays.sort(studentA1, new SortByRoll()); System.out.println("studentA1 array after sort: " + Arrays.asList(studentA1)); //sort method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. Student studentA2[] = {new Student(3, "Student3"), new Student(4, "Student4"), new Student(2, "Student2"), new Student(1, "Student1")}; System.out.println("studentA2 array before sort: " + Arrays.asList(studentA2)); Arrays.sort(studentA2, 1, 4, new SortByRoll()); System.out.println("studentA2 array after sort: " + Arrays.asList(studentA2)); //spliterator method returns a Spliterator covering all of the specified array. String stringA10[] = {"a", "b", "c", "d"}; System.out.print("spliterator result: "); Arrays.spliterator(stringA10).forEachRemaining(d -> System.out.print(d + " ")); System.out.println(); //spliterator method returns a Spliterator covering the specified range of the specified array. String stringA11[] = {"a", "b", "c", "d"}; System.out.print("spliterator with range result: "); Arrays.spliterator(stringA11, 1, 3).forEachRemaining(d -> System.out.print(d + " ")); System.out.println(); //stream method returns a sequential Stream with the specified array as its source. int intA15[] = {1, 2, 3, 4, 5}; System.out.print("stream with foreach result: "); Arrays.stream(intA15).forEach(e -> System.out.print(e + " ")); System.out.println(); //toString returns a string representation of the contents of the specified array. int intA16[] = {1, 2, 3, 4, 5}; System.out.println("toString method result: " + Arrays.toString(intA16)); }//end main }//end Class class Student { int roll; String name; public Student(int roll, String name) { this.roll = roll; this.name = name; } @Override public String toString() { return roll + " - " + name; }} class SortByRoll implements Comparator<Student> { @Override public int compare(Student a, Student b) { return a.roll - b.roll; }} Output Thank for visiting Please share if you like it