Arrays acquaintance of Java classes

Arrays acquaintance of Java classes

Arrays class including many static methods (such as sorting and searching) for operating the array, and static methods can be called directly by the class name Arrays. Arrays class before using need to import:

 import java.util.Arrays;

Benpian recording operation based on several subsequent waiting sophisticated technology, to be supplemented.

Binary search

  • public static int binarySearch(type[]a,type key)
int[] a = {1,2,3,4};
System.out.println(Arrays.binarySearch(a,1));//输出0  即1的索引值
System.out.println(Arrays.binarySearch(a,7));//输出-1 找不到就负数

Binary search index value in a key array, not found return negative; need array in ascending order.

  • public static int binarySearch(type[]a,int fromIndex,int toIndex,type key)
int[] b = {5,6,7,8};
System.out.println(Arrays.binarySearch(b,0,3,6));//返回1 b中0-3里有6就返回6的索引
System.out.println(Arrays.binarySearch(b,0,3,9));//返回负数 b中0-3里没有9,返回负数
System.out.println(Arrays.binarySearch(b,0,4,8));//返回3 不包括索引为4的值
System.out.println(Arrays.binarySearch(b,0,5,8));//报错,超出范围

A similar method, but the search index fromIndex to toIndex (excluding toIndex), index range to be noted.

Array copy

  • public static type[] copyOf(type[] original,int length)
int[] a = {1,2,3,4};
int[] c = Arrays.copyOf(a,5);
int[] d = Arrays.copyOf(a,3);
for(int m:d) System.out.print(m);//123
for(int m:c) System.out.print(m);//12340
String[] b = Arrays.copyOf(a,4)//报错,类型不符

Copy original array into a new array, length is the length of the new array, three cases:

  1. length> original.length, more 0s portion integer type, make reference type null, Boolean complement false.
  2. length == original.length, just copy.
  3. length <original.length, is copied before long element length.
  • public static type[] copyOfRange(type[]original,int from,int to)
int[] a ={1,2,3,4,5};
int[] b =Arrays.copyOfRange(a,1,3);
int[] c = Arrays.copyOfRange(a,0,6);
System.out.println(Arrays.toString(b));//[2,3]
System.out.println(Arrays.toString(c));//[1,2,3,4,5,0]

Similarly, just copy the original index into the array from the elements to the index.

Into a string

  • public static String toString(type[]a)
char[]b = {'a','b','c'};
System.out.println(Arrays.toString(b));//[a,b,c]

The type of the array into a string, the array elements are separated by a comma and a space sequentially.

Array filling

  • public static void fill(type[]a,type val)
String[] stringArray = new String[3];
Arrays.fill(stringArray,"he");
System.out.println(Arrays.toString(stringArray));//[he,he,he]

All the elements of the array filled with a val.

  • public static void fill(type[]a,int fromIndex,int toIndex,type val)
String[] stringArray = {"he","he","he"};
Arrays.fill(stringArray,0,1,"m");
System.out.println(Arrays.toString(stringArray));//[m,he,he]

ToIndex fromIndex to the element is filled with a val, to fill up a number of pre toIndex (not including toIndex).

Array Comparisons

  • public static boolean equals(type[]a,type[]a2)
int[] a = {5,6,7,8};
int[] b = {5,6,7,8};
int[] c = {5,6,7};
System.out.println(Arrays.equals(a, b));//true
System.out.println(Arrays.equals(a, c));//false

If a length of the array and a2 are equal and the array elements are also inside the same, return true.

Sorting an array

  • public static void sort(type[] a)
char[] d = {'d','a','c','b'};
Arrays.sort(d);
System.out.println(Arrays.toString(d));//[a,b,c,d]

Original array are arranged in ascending order.

  • public static void sort(type[]a,int fromIndex,int toIndex)
int[] a ={3,1,2,4};
Arrays.sort(a,0,2);
System.out.println(Arrays.toString(a));//[1,3,2,4]

Ascending order of the original array, in the range fromIndex toIndex arranged to, before the number of up to toIndex (exclusive toIndex).

In summary:

  • Array type element type must match the array operation between the need to match type.
  • fromIndex certain less toIndex.
  • fromIndex must be greater than zero.
  • toIndex need less than the length of the array.

Guess you like

Origin www.cnblogs.com/summerday152/p/11906729.html
Recommended