Arrays类常见方法案例
Arrays里面包含了一系列静态方法,用于管理或操作数组【比如排序和搜索】
1.toString 返回数组的字符串形式
Arrays.toString(arr);
2.sort 排序【自然排序和定制排序】[重点]
3.binarySearch 通过二分搜索法进行查找,要求必须排好序
int index = Arrays.binarySearch(arr,3);
4.copyOf 数组元素的复制
Integer newArr = Arrays.copyOf(arr,arr.length);
5.fill数组元素的填充
Integer[] num = new Integer[]{9,6,3};
Arrays.fill(num,8);//将数组num中的所有元素都替换成8
6.equals 比较两个数组元素内容是否完全一致
boolean equals = Arrays.equals(arr,arr2);//如果数组arr和arr2完全一样返回true,否则返回false
7.asList 将一组值,转换成List集合
List asList = Arrays.asList(2,3,43,5,6,78,168);
System.out.println("asList="+asList);
Arrays类常见方法案例
package wrapper_.arrays;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Arrays01 {
public static void main(String[] args) {
// 1.toString 返回数组的字符串形式
int[] arr = {11,22,33,43,55,88};
String str = Arrays.toString(arr);
System.out.println(str);//返回值:[11, 22, 33, 43, 55, 88]
// 2.sort 排序方法
Integer integers[] = {11,-1,7,9,0,168};
// Arrays.sort(integers);//默认是从小到大排序
System.out.println("=========排序后的结果=========");
// System.out.println(Arrays.toString(integers));
Arrays.sort(integers, new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
System.out.println(Arrays.toString(integers));
//binarySearch 通过二分搜索法进行查找 ,要求必须排好
Integer[] arrs = {1,2,3,5,6,78};
int search = Arrays.binarySearch(arrs, 5);
System.out.println(search);
//copyOf数组元素的复制
int[] newArr = Arrays.copyOf(arr, arr.length);
System.out.println(Arrays.toString(newArr));
//fill 数组元素的填充
Integer[] fillArr = {1,46,168};
Arrays.fill(fillArr,168);
//equals 比较两个数组 元素内容是否完全一致
int[] arr2 = {11,22,33,43,55,88};
boolean equals = Arrays.equals(arr, arr2);
System.out.println(equals);
// asList 将一组值,转换成List
List asList = Arrays.asList(1, 5, 168, 88, 69);
System.out.println("asList的运行类型是:"+asList.getClass());
//asList的运行类型是:class java.util.Arrays$ArrayList
}
}
模拟Arrays的动态绑定
package wrapper_.arrays;
import java.util.Arrays;
import java.util.Comparator;
public class Array02 {
public static void main(String[] args) {
int[] arr = {1,-2,8,168,99};
// arraySort(arr);
// System.out.println(Arrays.toString(arr));
arraySort02(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return i1 - i2;
}
});
System.out.println(Arrays.toString(arr));
}
public static void arraySort(int[] arr){
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i -1; j++) {
if (arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//实现定制排序
public static void arraySort02(int[] arr, Comparator c){
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1 -i; j++) {
if (c.compare(arr[j],arr[j+1])>0){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
Arrays类练习题目
package wrapper_.arrays;
import java.util.Arrays;
import java.util.Comparator;
public class ArraysExercise {
public static void main(String[] args) {
Book[] books = new Book[4];
books[0] = new Book("大笑江湖",168);
books[1] = new Book("java从入门到入土",81688);
books[2] = new Book("SpringBoot",5);
books[3] = new Book("小学生",12);
// mySort(books, new Comparator() {
// @Override
// public int compare(Object o1, Object o2) {
// double i1 = (double) o1;
// double i2 = (double) o2;
// return (int) (i1 - i2);
// }
// });
sortLength(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return i1 - i2;
}
});
System.out.println(Arrays.toString(books));
}
//对price的大小进行排列
public static void mySort(Book[] books, Comparator c){
Book temp = null;
for (int i = 0; i < books.length -1; i++) {
for (int j = 0; j < books.length - 1 -i; j++) {
if (c.compare(books[j].getPrice(),books[j+1].getPrice())>0){
temp = books[j];
books[j] = books[j+1];
books[j+1] = temp;
}
}
}
}
//对书名长度进行排序
public static void sortLength(Book[] books,Comparator comparator){
Book temp = null;
for (int i = 0; i < books.length-1; i++) {
for (int j = 0; j < books.length - 1 -i; j++) {
if (comparator.compare(books[j].getName().length(),
books[j+1].getName().length())>0){
temp = books[j];
books[j] = books[j+1];
books[j+1] = temp;
}
}
}
}
}
class Book {
private String name;
private double price;
public Book() {
}
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + ''' +
", price=" + price +
'}';
}
}
System类
System类常见方法和案例
1.exit: 退出当前程序
2.arraycopy:复制数组元素,比较适合底层调用,一般使用Arrays.copyOf完成复制数组
int[] scr = {1,2,3};
int[] dest = new int[3];
System.arraycopy(src,0,dest,0,3);
3.currentTimeMillens:返回当前时间距离1970-1-1的毫秒数
4.gc:运行垃圾回收机制System.gc();
package wrapper_.system;
import java.util.Arrays;
public class System01 {
public static void main(String[] args) {
// exit 退出当前程序
System.out.println("ok01");
// System.exit(0);
System.out.println("ok02");
int[] arr = {1,2,3};
int[] newArr = new int[arr.length];
System.arraycopy(arr,0,newArr,0,arr.length);
System.out.println(Arrays.toString(newArr));
//currentTimeMillens:返回当前时间距离 1970-01-01的毫秒数
System.out.println(System.currentTimeMillis());
}
}



