catalogue
- Java common class library arrays
- 1、 Common methods
- 1.1 toString
- 1.2 Sort
- 1.3 copyOf
- 1.4 mismatch
- 1.5 binarySearch
- 1.5.1 binarySearch(T[] a, int fromIndex, int toIndex, T key)
- 1.5.2 binarySearch(T[] a, T key)
- 1.5.3 others
- 1.6 equals
- 1.6.1 equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
- 1.6.2 equals(T[] a, T[] a2)
- 1.6.3 others
- 1.7 fills
- 2、 Other methods
- summary
Java common class library arrays
The arrays class contains various methods for manipulating arrays, such as sorting and searching
- If the specified array reference is null, all methods in this class throw NullPointerException unless otherwise noted
1、 Common methods
1.1 toString
Returns the contents of the specified arrayString form
give an example
int[] a1 = {1,2,3,4,5};
System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
Source code
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
other
Modifier and Type | Field | Description |
---|---|---|
static String | deepToString(Object[] a) | Returns a string representation of the “deep content” of the specified array |
1.2 Sort
Sort (default ascending)
1.2.1 sort(T[] a, int fromIndex, int toIndex)
Specify an interval to sort
give an example
int[] a1 = {9,1,3,7,2,5};
System.out.println(Arrays.toString(a1));//[9, 1, 3, 7, 2, 5]
Arrays.sort(a1,0,3);// [0,3), sort 9,1,3
System.out.println(Arrays.toString(a1));//[0[1, 3, 9, 7, 2, 5]
Source code
public static void sort(int[] a, int fromIndex, int toIndex) {
rangeCheck(a.length, fromIndex, toIndex);
DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
}
1.2.2 Sort(T[] a)
Sort the entire array
give an example
int[] a1 = {0,7,8,2,4,1};
System.out.println(Arrays.toString(a1));//[0, 7, 8, 2, 4, 1]
Arrays.sort(a1);
System.out.println(Arrays.toString(a1));//[0, 1, 2, 4, 7, 8]
Source code
public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}
1.2.3 others
Modifier and Type | Field | Description |
---|---|---|
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 raised by the specified comparator. |
static void | sort(T[] a, Comparator<? super T> c) | Sorts the specified array of objects according to the order raised by the specified comparator. |
static void | parallelSort(T[] a) | Sorts the specified array in ascending order. |
static void | parallelSort(T[] a, int fromIndex, int toIndex) | Sorts the specified array range in ascending numerical order. |
static <T extends Comparable<? super T>>void | parallelSort(T[] a) | Sorts the specified array of objects in ascending order according to the natural ordering of the element. |
static <T extends Comparable<? super T>>void | parallelSort(T[] a, int fromIndex, int toIndex) | Sorts the specified range of the specified object array in ascending order according to the natural ordering of the element. |
static void | parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) | Sorts the specified range of the specified array of objects according to the order raised by the specified comparator. |
static void | parallelSort(T[] a, Comparator<? super T> cmp) | Sorts the specified array of objects according to the order raised by the specified comparator. |
1.3 copyOf
Copy (often used for array expansion)
give an example
int[] a = {1,2,3};
System.out.println(a.length);//output:3
a = Arrays.copyOf(a,15);
System.out.println(a.length);//output:15
Source code
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
other
Modifier and Type | Field | Description |
---|---|---|
static T[] | copyOf(T[] original, int newLength) | Copies the specified array with a null value, truncates or fills, if necessary, so that the copy has the specified length |
static <T,U>T[] | Copyof (u [] original, int newlength, class <? Extensions t [] > newtype) | Copies the specified array with a null value, truncates or fills, if necessary, so that the copy has the specified length |
1.4 mismatch
give an example
int[] a1 = {0,1,2,3,4,5};
int[] a2 = {0,1,2,3,4,5};// Same as A1
int[] a3 = {0,1,2,3,0,5};// Starting from index 4, it is different from A1
System.out.println(Arrays.mismatch(a1,a2));//output:-1
System.out.println(Arrays.mismatch(a1,a3));//output:4
Source code
public static int mismatch(int[] a, int[] b) {
int length = Math.min(a.length, b.length); // Check null array refs
if (a == b)
return -1;
int i = ArraysSupport.mismatch(a, b, length);
return (i < 0 && a.length != b.length) ? length : i;
}
other
Modifier and Type | Field | Description |
---|---|---|
static int | mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | Find and return the relative index of the first mismatch between two object arrays within the specified range. Otherwise, if no mismatch is found, return – 1. |
static int | mismatch(T[] a, T[] b, Comparator<? super T> cmp) | Find and return the index of the first mismatch between two object arrays. Otherwise, if no mismatch is found, return – 1. |
1.5 binarySearch
Binary search, search, return subscript
1.5.1 binarySearch(T[] a, int fromIndex, int toIndex, T key)
Limits the scope of the search [fromindex, toindex)
give an example
int[] a = {1,2,3,4,5};
int x1 = Arrays.binarySearch(a,2,3,4);// Find a subscript with a value of 4 in the a array subscript [2,3]
System.out.println(x1);// Output: < 0 random number
Source code
public static int binarySearch(int[] a, int fromIndex, int toIndex,int key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
1.5.2 binarySearch(T[] a, T key)
It is the same as the above, but there is no limited range, fromindex = 0, toindex = length
give an example
int[] a = {1,2,3,4,5};
int x1 = Arrays.binarySearch(a,3);// Finds a subscript with a value of 3 in the a array
int x2 = Arrays.binarySearch(a,-6);// Finds a subscript with a value of 6 in the a array
System.out.println(x1);//output:2
System.out.println(x2);// Output: < 0 random number
Source code
public static int binarySearch(int[] a, int key) {
return binarySearch0(a, 0, a.length, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
1.5.3 others
Modifier and Type | Field | Description |
---|---|---|
static int | binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) | Searches the specified array range of the specified object using a binary search algorithm |
static int | binarySearch(T[] a, T key, Comparator<? super T> c) | Searches the specified array for the specified object using a binary search algorithm |
1.6 equals
1.6.1 equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
Returns true if two specified arrays are equal within the specified range
give an example
int[] a1 = {1,2,3,4,5};
int[] a2 = {1,2,0,0,4,5};
System.out.println(Arrays.equals(a1,0,2,a2,0,2));//true
System.out.println(Arrays.equals(a1,3,5,a2,4,6));//true
Source code
public static boolean equals(int[] a, int aFromIndex, int aToIndex,int[] b, int bFromIndex, int bToIndex) {
rangeCheck(a.length, aFromIndex, aToIndex);
rangeCheck(b.length, bFromIndex, bToIndex);
int aLength = aToIndex - aFromIndex;
int bLength = bToIndex - bFromIndex;
if (aLength != bLength)
return false;
return ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex,aLength) < 0;
}
1.6.2 equals(T[] a, T[] a2)
Returns true if two specified arrays are equal
give an example
int[] a1 = {1,2,3,4,5};
int[] a2 = {1,2,3,4,5};
int[] a3 = {1,2,0,4,5};
System.out.println(Arrays.equals(a1,a2));//true
System.out.println(Arrays.equals(a1,a3));//false
Source code
public static boolean equals(int[] a, int[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
return ArraysSupport.mismatch(a, a2, length) < 0;
}
1.6.3 others
Modifier and Type | Field | Description |
---|---|---|
static boolean | deepEquals(Object[] a1, Object[] a2) | Returns true if two specified arrays are equal in depth to each other |
static boolean | equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | Returns true if the two object arrays specified within the specified range are equal to each other |
static boolean | equals(T[] a, T[] a2, Comparator<? super T> cmp) | Returns true if two specified objects arrays are equal to each other |
1.7 fills
1.7.1 fill(T[] a, int fromIndex, int toIndex, T val)
Assigns the specified t value to each element of the specified range of the specified array of T types
give an example
int[] a1 = new int[10];
Arrays.fill(a1,1,4,8);
char[] a2 = new char[10];
Arrays.fill(a2,0,3,'s');
System.out.println(Arrays.toString(a1));//[0, 8, 8, 8, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(a2));//[s, s, s, , , , , , , ]
Source code
public static void fill(char[] a, int fromIndex, int toIndex, char val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}
1.7.2 fill(T[] a, T val)
Assigns the specified t value to each element of the specified array of T types
give an example
int[] a1 = new int[10];
Arrays.fill(a1,8);
char[] a2 = new char[10];
Arrays.fill(a2,'s');
System.out.println(Arrays.toString(a1));//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
System.out.println(Arrays.toString(a2));//[s, s, s, s, s, s, s, s, s, s]
Source code
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
2、 Other methods
Modifier and Type | Field | Description |
---|---|---|
static List | asList(T… a) | Returns a list of fixed sizes supported by the specified array. |
static int | compare(T[] a, T[] b) | Dictionary order compares two t arrays |
static int | compare(T[] a, int aFromIndex, int aToIndex,T[] b, int bFromIndex, int bToIndex) | Compares two t-arrays in dictionary order within a specified range |
static <T extends Comparable<? super T>>int | compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) | Compares two object arrays in dictionary order within the specified range. |
static int | compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | Compares two object arrays in dictionary order within the specified range. |
static <T extends Comparable<? super T>>int | compare(T[] a, T[] b) | Compare two object arrays in object order in comparable elements. |
static int | compare(T[] a, T[] b, Comparator<? super T> cmp) | Compares two object arrays in dictionary order using the specified comparator |
static int | compareUnsigned(T[] a, T[] b) | Byte dictionary compares two t arrays sequentially, and the number processing elements are unsigned |
static int | compareUnsigned(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) | Compares two t arrays in dictionary order within the specified range, and processes the element numbers as unsigned |
static T[] | copyOfRange(T[] original, int from, int to) | Copies the specified range of the specified array to the new array |
static T[] | copyOfRange(T[] original, int from, int to) | Copies the specified range of the specified array to the new array |
static <T,U>T[] | Copyofrange (u [] original, int from, int to, class <? Extensions t [] > newtype) | Copies the specified range of the specified array to the new array |
static int | hashCode(T[] a) | Returns a hash code based on the contents of the specified array |
static int | deepHashCode(Object[] a) | Returns a hash code based on the “deep content” of the specified array |
static void | parallelPrefix(T[] array, int fromIndex, int toIndex,TBinaryOperator op) | Execute parallelprefix (t []) for the given array sub range,TBinaryOperator) |
static void | parallelPrefix(dT[] array, TBinaryOperator op) | Accumulate each element of a given array in parallel using the provided function |
static void | parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator op) | Execute parallelprefix (object [], binaryoperator) for the given array sub range |
static void | parallelPrefix(T[] array, BinaryOperator op) | Accumulate each element of a given array in parallel using the provided function |
static void | parallelSetAll(double[] array, IntToDoubleFunction generator) | Use the provided generator function to set all elements of the specified array in parallel to evaluate each element |
static void | parallelSetAll(int[] array, IntUnaryOperator generator) | Use the provided generator function to set all elements of the specified array in parallel to evaluate each element |
static void | parallelSetAll(long[] array, IntToLongFunction generator) | Use the provided generator function to set all elements of the specified array in parallel to evaluate each element |
static void | parallelSetAll(T[] array, IntFunction<? extends T> generator) | Use the provided generator function to set all elements of the specified array in parallel to evaluate each element |
static void | setAll(double[] array, IntToDoubleFunction generator) | Use the supplied generator function to set all the elements of the specified array to evaluate each element |
static void | setAll(int[] array, IntUnaryOperator generator) | Use the supplied generator function to set all the elements of the specified array to evaluate each element |
static void | setAll(long[] array, IntToLongFunction generator) | Use the supplied generator function to set all the elements of the specified array to evaluate each element |
static void | setAll(T[] array, IntFunction<? extends T> generator) | Use the supplied generator function to set all the elements of the specified array to evaluate each element |
static | Spliterator.OfDouble spliterator(double[] array) | Returns splitter.ofdouble that overwrites all specified arrays |
static | Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) | Returns splitter.ofdouble that overrides the specified range of the specified array |
static | Spliterator.OfInt spliterator(int[] array) | Returns splitter.ofint that overwrites all specified arrays |
static | Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) | Returns splitter.ofint that overrides the specified range of the specified array |
static | Spliterator.OfLong spliterator(long[] array) | Returns splitter.oflong that overwrites all specified arrays |
static | Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) | Returns splitter.oflong that overrides the specified range of the specified array |
static Spliterator | spliterator(T[] array) | Returns the splitter that overwrites all specified arrays |
static Spliterator | spliterator(T[] array, int startInclusive, int endExclusive) | Returns the splitter that overrides the specified range of the specified array |
static DoubleStream | stream(double[] array) | Returns a doublestream in the order in which the specified array is the source |
static DoubleStream | stream(double[] array, int startInclusive, int endExclusive) | Returns the specified range of the specified array as the sequential doublestream of its source |
static IntStream | stream(int[] array) | Returns the order intstream with the specified array as the source |
static IntStream | stream(int[] array, int startInclusive, int endExclusive) | Returns the specified range of the specified array as the sequential intstream of its source |
static LongStream | stream(long[] array) | Returns the order longstream with the specified array as the source |
static LongStream | stream(long[] array, int startInclusive, int endExclusive) | Returns the specified range of the specified array as the order longstream of its source |
static Stream | stream(T[] array) | Returns an ordered stream with the specified array as the source |
static Stream | stream(T[] array, int startInclusive, int endExclusive) | Returns the ordered stream of the specified range of the specified array as its source |
summary
That’s all for this article. I hope it can help you and pay more attention to developeppaer!