Arrays
操作數組的工具類。
Arrays.toString()
import java.util.Arrays;
public class test40 {
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
String str = Arrays.toString(ints);
System.out.println(str); //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
}
Arrays.binarySearch()
import java.util.Arrays;
public class test40 {
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int index = Arrays.binarySearch(ints, 10);
System.out.println(index);
}
}
Arrays.copyOf()
如果新數組的長度小于原始數組的長度——部分拷貝
如果新數組的長度等于原始數組的長度——完全拷貝
如果新數組的長度大于原始數組的長度——完全拷貝+補上默認初始化值0
import java.util.Arrays;
public class test40 {
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] ints1 = Arrays.copyOf(ints, 5);
System.out.println(Arrays.toString(ints1)); //[1, 2, 3, 4, 5]
}
}
Arrays.copyOfRange()
范圍:包頭不包尾
import java.util.Arrays;
public class test40 {
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] ints1 = Arrays.copyOfRange(ints, 3, 5);
System.out.println(Arrays.toString(ints1)); //[4, 5]
}
}
Arrays.fill()
import java.util.Arrays;
public class test40 {
public static void main(String[] args) {
int[] ints=new int[10];
Arrays.fill(ints,0);
System.out.println(Arrays.toString(ints)); //[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}
}
Arrays.sort()
默認使用升序排序。
底層使用的是快速排序。
import java.util.Arrays;
public class test40 {
public static void main(String[] args) {
int[] ints = {5, 4, 8, 2, 4, 7, 6, 3, 0, 1, 9, 10};
Arrays.sort(ints);
System.out.println(Arrays.toString(ints)); //[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10]
}
}
要想改變排序順序:
第二個參數是一個接口,在調用方法的時候,要傳遞這個接口的實現類對象,作為排序的規(guī)則。
底層原理:插入排序+二分查找
默認把0索引的數據當作是有序的序列,1索引到最后是無序的序列。
遍歷無序序列里面的每一個元素,假設當前遍歷的元素是A,
利用二分查找確定A的插入點,拿著A跟插入點的元素進行比較,
比較的規(guī)則是Comparator<>(),
如果方法返回值是負數,拿著A繼續(xù)跟前面的數據進行比較,
如果方法返回值是正數,拿著A繼續(xù)跟后面的數據進行比較,
如果方法返回值是0,拿著A繼續(xù)跟后面的數據進行比較,
直到確定A的最終位置。
compare(Integer o1, Integer o2):
o1:表示在無序序列中,遍歷得到的每一個元素
o2:有序序列中的元素
返回值:
負數:表示當前要插入的元素小,放在前面
正數:表示當前要插入的元素大,放在后面文章來源:http://www.zghlxwxcb.cn/news/detail-812204.html
0:表示當前要插入的元素跟當前元素一樣,也是放在后面文章來源地址http://www.zghlxwxcb.cn/news/detail-812204.html
升序排序
import java.util.Arrays;
import java.util.Comparator;
public class test40 {
public static void main(String[] args) {
Integer[] integers = {5, 4, 8, 2, 4, 7, 6, 3, 0, 1, 9, 10};
Arrays.sort(integers, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
System.out.println(Arrays.toString(integers)); //[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10]
}
}
降序排序
import java.util.Arrays;
import java.util.Comparator;
public class test40 {
public static void main(String[] args) {
Integer[] integers = {5, 4, 8, 2, 4, 7, 6, 3, 0, 1, 9, 10};
Arrays.sort(integers, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
System.out.println(Arrays.toString(integers)); //[10, 9, 8, 7, 6, 5, 4, 4, 3, 2, 1, 0]
}
}
到了這里,關于Java學習筆記(七)——操作數組工具類Arrays的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!