Chapters
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists. The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
In this tutorial, I'm gonna demonstrate some of the methods of Arrays class. More information can be found in the Arrays documentation.
This method copies array elements and put the elements in a List.
Method form:
This example demonstrates asList() method.
This method searches an array for a particular value. The content of the array must be sorted(as by the
This method returns the index of the specified value. This method uses binary search algorithm. This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
First parameter is a byte array. Second parameter is a byte value that we wanna search.
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
This method compares two arrays lexicographically. This method returns 0 if the elements of both arrays are equal and the length of both arrays are equal. Returns an negative number if one of the elements of array "a" is less than one of the elements of array "b" or array "a" length is less than array "b" length. Otherwise, returns a positive number.
The third result is -5 because 50 at index 1 of b2 and 55 at index 1 of b4 are not equal. The number 5 comes from the difference(b2[1]-b4[1]) of the values of b2 and b4.
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
This method compares two byte arrays lexicographically, numerically treating elements as unsigned. This method returns 0 if the elements of both arrays are equal and the length of both arrays are equal. Returns an negative number if one of the elements of array "a" is less than one of the elements of array "b" or array "a" length is less than array "b" length. Otherwise, returns a positive number.
The third result is -255 because in java, negative numbers are represented via two's complement. In signed byte, -1 is 11111111 in binary. Since
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain (byte)0. Such indices will exist if and only if the specified length is greater than that of the original array.
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
This method returns true if two arrays are equal or both null. Otherwise, returns false. Two arrays are equal if they have the same number of length and elements.
Method form:
This method returns true if two Object type arrays are deeply equal. Otherwise, returns false. Two arrays are deeply equal if they're both null; have the same type, length and elements.
This method assigns a specified value to each element of an array. This method has many forms that are similar to one another. In this topic I'm gonna demonstrate this form:
Method form:
Finds and returns the index of the first mismatch between two byte arrays, otherwise return -1 if no mismatch is found. The index will be in the range of 0 (inclusive) up to the length (inclusive) of the smaller array. If one of the arrays is smaller than the other one; the elements of the small array are equal to the portion of the large array starting from 0 up to the maximum index of the small aray, this method returns the length of the small array.
This method has many forms that are similar to one another. In this topic I'm gonna demonstrate this form:
Method form:
Cumulates, in parallel, each element of the given array in place, using the supplied function. For example if the array initially holds
This method implements parallelism. Thus, this method benefits from multicore systems and can process large arrays more faster than sequential loops. This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
This example demonstrates
Sorts the specified array into ascending numerical order if the elements are numbers. This method sorts Strings according to the natural ordering. This method implements parallelism. Thus, this method benefits from multicore systems and can sort large arrays more faster than
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
Set all elements of the specified array, using the provided generator function to compute each element. The generator uses the array indexes to generate new elements. If the generator function throws an exception, it is relayed to the caller and the array is left in an indeterminate state.
Arrays Class
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists. The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
In this tutorial, I'm gonna demonstrate some of the methods of Arrays class. More information can be found in the Arrays documentation.
copyOfRange()
and sort()
are demonstrated in this article.
asList() Method
This method copies array elements and put the elements in a List.
Method form:
asList(T... a)
This example demonstrates asList() method.
import java.util.Arrays; import java.util.ArrayList; public class SampleClass{ public static void main(String[] args){ String[] str = {"Apple","Candy","Mango","Coconut"}; ArrayList<String> strCol = new ArrayList<>(Arrays.asList(str)); for(String s : strCol) System.out.println(s); } }
binarySearch() Method
This method searches an array for a particular value. The content of the array must be sorted(as by the
sort()
) prior to calling this method. Otherwise, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
This method returns the index of the specified value. This method uses binary search algorithm. This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
binarySearch(byte[] a, byte key)
First parameter is a byte array. Second parameter is a byte value that we wanna search.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ byte[] bytes = {10,50,33,11}; byte target = 33; Arrays.sort(bytes); int index = Arrays.binarySearch(bytes, target); System.out.println("33 is at index: " + index); } } Result 33 is at index: 2
compare() Method
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
compare(byte[] a, byte[] b)
This method compares two arrays lexicographically. This method returns 0 if the elements of both arrays are equal and the length of both arrays are equal. Returns an negative number if one of the elements of array "a" is less than one of the elements of array "b" or array "a" length is less than array "b" length. Otherwise, returns a positive number.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ byte[] b1 = {10,50,33,11}; byte[] b2 = {10,50,33,11}; byte[] b3 = {10,50,33}; byte[] b4 = {10,55,33,11}; System.out.println("Compare b1 and b2..."); System.out.println("Result: " + Arrays.compare(b1,b2)); System.out.println("Compare b1 and b3..."); System.out.println("Result: " + Arrays.compare(b1,b3)); System.out.println("Compare b2 and b4..."); System.out.println("Result: " + Arrays.compare(b2,b4)); } } Result Compare b1 and b2... Result: 0 Compare b1 and b3... Result: 1 Compare b2 and b4... Result: -5The first result is 0 because b1 and b2 are equal in terms of length and elements. The second result is 1 because b1 and b3 have equal elements but their length is not the same. The number 1 comes from the difference(b1.length-b3.length) of the length of b1 and b3.
The third result is -5 because 50 at index 1 of b2 and 55 at index 1 of b4 are not equal. The number 5 comes from the difference(b2[1]-b4[1]) of the values of b2 and b4.
compareUnsigned() Method
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
compareUnsigned(byte[] a, byte[] b)
This method compares two byte arrays lexicographically, numerically treating elements as unsigned. This method returns 0 if the elements of both arrays are equal and the length of both arrays are equal. Returns an negative number if one of the elements of array "a" is less than one of the elements of array "b" or array "a" length is less than array "b" length. Otherwise, returns a positive number.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ byte[] b1 = {0,50,33,11}; byte[] b2 = {0,50,33,11}; byte[] b3 = {0,50,33}; byte[] b4 = {-1,55,33,11}; System.out.println("Compare b1 and b2..."); System.out.println("Result: " + Arrays.compareUnsigned(b1,b2)); System.out.println("Compare b1 and b3..."); System.out.println("Result: " + Arrays.compareUnsigned(b1,b3)); System.out.println("Compare b2 and b4..."); System.out.println("Result: " + Arrays.compareUnsigned(b2,b4)); } } Result Compare b1 and b2... Result: 0 Compare b1 and b3... Result: 1 Compare b2 and b4... Result: -255The first result is 0 because b1 and b2 are equal in terms of length and elements. The second result is 1 because b1 and b3 have equal elements but their length is not the same. The number 1 comes from the difference(b1.length-b3.length) of the length of b1 and b3.
The third result is -255 because in java, negative numbers are represented via two's complement. In signed byte, -1 is 11111111 in binary. Since
compareUnsigned(byte[] a, byte[] b)
treats bytes as unsigned, 11111111 is equal to 255. The unequal values(0 and 255) are converted to int before subtracting them. So, 0 - 255 = -255.
copyOf() Method
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
public static byte[] copyOf(byte[] original, int newLength)
Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain (byte)0. Such indices will exist if and only if the specified length is greater than that of the original array.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ byte[] b1 = {10, 20, 30, 40, 50}; byte[] b2 = Arrays.copyOf(b1,6);; System.out.println("b2 elements..."); for(byte b : b2) System.out.print(b + " "); } } Result b2 elements... 10 20 30 40 50 0
equals() Method
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
equals(byte[] a, byte[] a2)
This method returns true if two arrays are equal or both null. Otherwise, returns false. Two arrays are equal if they have the same number of length and elements.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ byte[] b1 = {10,50,33,11}; byte[] b2 = {10,50,33,11}; byte[] b3 = {10,50,33}; byte[] b4 = {20,50,33,11}; System.out.println("Compare b1 and b2..."); System.out.println("Result: " + Arrays.equals(b1,b2)); System.out.println("Compare b1 and b3..."); System.out.println("Result: " + Arrays.equals(b1,b3)); System.out.println("Compare b2 and b4..."); System.out.println("Result: " + Arrays.equals(b2,b4)); } } Result Compare b1 and b2... Result: true Compare b1 and b3... Result: false Compare b2 and b4... Result: false
deepEquals() Method
Method form:
deepEquals(Object[] a1, Object[] a2)
This method returns true if two Object type arrays are deeply equal. Otherwise, returns false. Two arrays are deeply equal if they're both null; have the same type, length and elements.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ Integer[] i1 = {2,4,6,8,10}; Integer[] i2 = {2,4,6,8,10}; Byte[] b1 = {2,4,6,8,10}; System.out.println("Compare i1 and b1..."); System.out.println("Result: " + Arrays.deepEquals(i1,b1)); System.out.println("Compare i1 and i2..."); System.out.println("Result: " + Arrays.deepEquals(i1,i2)); } } Result Compare i1 and b1... Result: false Compare i1 and i2... Result: true
fill() Method
This method assigns a specified value to each element of an array. This method has many forms that are similar to one another. In this topic I'm gonna demonstrate this form:
Method form:
fill(int[] a, int val)
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ int[] ints = {2,4,6,8,10}; System.out.println("Current elements..."); for(int i : ints) System.out.print(i + " "); System.out.println(); System.out.println("Fill with 3s"); Arrays.fill(ints,3); System.out.println("New elements..."); for(int i : ints) System.out.print(i + " "); } } Result Current elements... 2 4 6 8 10 Fill with 3s New elements... 3 3 3 3 3
mismatch() Method
Finds and returns the index of the first mismatch between two byte arrays, otherwise return -1 if no mismatch is found. The index will be in the range of 0 (inclusive) up to the length (inclusive) of the smaller array. If one of the arrays is smaller than the other one; the elements of the small array are equal to the portion of the large array starting from 0 up to the maximum index of the small aray, this method returns the length of the small array.
This method has many forms that are similar to one another. In this topic I'm gonna demonstrate this form:
Method form:
mismatch(byte[] a, byte[] b)
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ byte[] b1 = {2,4,6,8,10}; byte[] b2 = {2,4,6,8}; byte[] b3 = {2,4,6,15}; byte[] b4 = {2,4,6,8,10}; System.out.println("Find a mismatch "+ "between b1 to b2..."); System.out.println("Result: " + Arrays.mismatch(b1,b2)); System.out.println("Find a mismatch "+ "between b1 to b3..."); System.out.println("Result: " + Arrays.mismatch(b1,b3)); System.out.println("Find a mismatch "+ "between b1 to b4..."); System.out.println("Result: " + Arrays.mismatch(b1,b4)); } } Result Find a mismatch between b1 to b2... Result: 4 Find a mismatch between b1 to b3... Result: 3 Find a mismatch between b1 to b4... Result: -1
parallelPrefix() Method
Cumulates, in parallel, each element of the given array in place, using the supplied function. For example if the array initially holds
[2, 1, 0, 3]
and the operation performs addition, then upon return the array holds[2, 2+1, 3+0, 3+3] = {2, 3, 3, 6}
.
This method implements parallelism. Thus, this method benefits from multicore systems and can process large arrays more faster than sequential loops. This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
parallelPrefix(int[] array, IntBinaryOperator op)
This example demonstrates
parallelPrefix()
.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ int[] ints = {2,4,6,8,10}; System.out.println("Current elements..."); for(int i : ints) System.out.print(i + " "); Arrays.parallelPrefix(ints, (num1,num2) -> num1 + num2); System.out.println(); System.out.println("New elements..."); for(int i : ints) System.out.print(i + " "); } } Result Current elements... 2 4 6 8 10 New elements... 2 6 12 20 30The example above demonstrates the functionality of
parallelPrefix()
. However, It's recommended to use sequential loop if we're just processing small arrays.
parallelSort() Method
Sorts the specified array into ascending numerical order if the elements are numbers. This method sorts Strings according to the natural ordering. This method implements parallelism. Thus, this method benefits from multicore systems and can sort large arrays more faster than
sort()
.
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
parallelSort(int[] a)
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ int[] ints = {2,6,4,8,12,10,20,16}; System.out.println("No sort"); for(int i : ints) System.out.print(i + " "); System.out.println(); System.out.println("With sort"); Arrays.parallelSort(ints); for(int i : ints) System.out.print(i + " "); } } Result No sort 2 6 4 8 12 10 20 16 With sort 2 4 6 8 10 12 16 20Take note, use
parallelSort()
if your target system has multiple processor ocres and you're sorting a large array. Otherwise, use sort()
. The example above is just a mere demonstration.
setAll() Method
This method has many forms that are similar to one another. In this topic, I'm gonna demonstrate this form:
Method form:
setAll(int[] array, IntUnaryOperator generator)
Set all elements of the specified array, using the provided generator function to compute each element. The generator uses the array indexes to generate new elements. If the generator function throws an exception, it is relayed to the caller and the array is left in an indeterminate state.
import java.util.Arrays; public class SampleClass{ public static void main(String[] args){ int[] arr = new int[5]; System.out.println("Set elements..."); Arrays.setAll(arr, (num1) -> num1); for(int i : arr) System.out.print(i + " "); arr = new int[5]; System.out.println(); System.out.println("Set squared numbers..."); Arrays.setAll(arr, (num1) -> (num1+1)*(num1+1)); for(int i : arr) System.out.print(i + " "); } } Result Set elements... 0 1 2 3 4 Set squared numbers... 1 4 9 16 25Use
parallelSetAll()
as an alternative to setAll()
if you want to take advantage of multicore systems and process large arrays.
No comments:
Post a Comment