Arrays
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;
public class ArraysTest1 {
public static void main(String[] args) {
// Arrays 常用方法
//1.public static String toString(类型[] arr):返回数组的内容
int[] arr = {1, 2, 3, 4, 5, 6, 7};
String rs = Arrays.toString(arr);
System.out.println(rs);
//2.public static 类型[] copyOfRange(类型[] arr,起始索引,结束索引):拷贝数组(指定范围,包前不包后)
int[] arr2 = Arrays.copyOfRange(arr, 4, arr.length);
System.out.println(Arrays.toString(arr2));
//3.public static copyOf(类型[] arr,int newlength):拷贝数组(可以指定新数组的长度)
int[] arr3 = Arrays.copyOf(arr, 10);
System.out.println(Arrays.toString(arr3));
//4.public static setAll(double[] array,IntToDoubleFunction generator):把数组中的原数据改为新数据又存进去
double[] prices = {67.8, 10.5, 23.4};
// 把所有的价格打八折,然后又存进去
//Arrays.setAll(prices, i -> prices[i] * 0.8);
Arrays.setAll(prices, new IntToDoubleFunction() {
@Override
public double applyAsDouble(int value) {
return prices[value] * 0.8;
}
});
System.out.println(Arrays.toString(prices));
//5.public static void sort(类型[] arr):排序(默认升序)
Arrays.sort(prices);
System.out.println(Arrays.toString(prices));
}
}
Student类
package com.mytest.d5_arrays;
public class Student implements Comparable<Student> {
private String name;
private int age;
private double height;
// 制定比较规则
// this o
@Override
public int compareTo(Student o) {
// 约定 左边对象大于右边对象,返回正整数
// 约定 左边对象小于右边对象,返回负整数
// 约定 左边对象等于右边对象,返回0
// 按年龄升序
/*if (this.age > o.age) {
return 1;
} else if (this.age < o.age) {
return -1;
}
return 0;*/
//return this.age - o.age;//升序
return o.age - this.age;//降序
}
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" + "name='" + name + '\'' + ", age=" + age + ", height=" + height + '}';
}
}
import java.util.Arrays;
import java.util.Comparator;
public class ArraysTest2 {
public static void main(String[] args) {
// 对数组中的对象排序
Student[] students = new Student[4];
students[0] = new Student("Tom",18, 125.5);
students[1] = new Student("Jerry", 20, 135.4);
students[2] = new Student("Alex", 19, 145.3);
students[3] = new Student("Mike", 21, 155.2);
// 1.public static void(类型[] arr): 对数组进行排序
// Arrays.sort(students);
// System.out.println(Arrays.toString(students));
// 2.public static <T> void sort(T[] arr,Comparator<? super T> c)
// 参数1:需要排序的数组
// 参数2:比较器,用于指定排序规则
/*Arrays.sort(students, (o1, o2) -> {
if (o1.getHeight() > o2.getHeight()) {
return 1;
} else if (o1.getHeight() < o2.getHeight()) {
return -1;
} else {
return 0;
}
});*/
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 左边对象o1 右边对象o2
// 约定 左边对象大于右边对象,返回正整数
// 约定 左边对象小于右边对象,返回负整数
// 约定 左边对象等于右边对象,返回0
// if (o1.getHeight() > o2.getHeight()) {
// return 1;
// } else if (o1.getHeight() < o2.getHeight()) {
// return -1;
// } else {
// return 0;
// }
return Double.compare(o1.getHeight(), o2.getHeight());//升序
//return Double.compare(o2.getHeight(), o1.getHeight());//降序
}
});
System.out.println(Arrays.toString(students));
}
}
Lambda表达式
public class LambdaTest1 {
public static void main(String[] args) {
// 认识 Lambda 表达式
// Animal a =new Animal(){
// @Override
// public void run() {
// System.out.println("狗在跑");
// }
// };
// a.run();
// 注意 Lambda 表达式并不是说能简化全部匿名内部类的写法,只能简化函数式接口的匿名内部类 (函数式接口:有且仅有一个抽象方法的接口)
// 错误代码 Animal不是一个接口
// Animal a = () -> {
// System.out.println("狗在跑");
// };
// a.run();
People p = () -> {
System.out.println("人正在吃饭");
};
p.eat();
// Swimming s = new Swimming() {
// @Override
// public void swim() {
// System.out.println("人正在游泳");
// }
// };
Swimming s = () -> {
System.out.println("人正在游泳");
};
s.swim();
}
}
abstract class Animal {
public abstract void run();
}
abstract interface People {
public abstract void eat();
}
interface Swimming {
public void swim();
}
import com.mytest.d5_arrays.Student;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.IntToDoubleFunction;
public class LambdaTest2 {
public static void main(String[] args) {
double[] prices = {99.8,128,100};
// Arrays.setAll(prices, new IntToDoubleFunction() {
// @Override
// public double applyAsDouble(int i) {
// return prices[i] * 0.9;
// }
// });
// Arrays.setAll(prices, (int i) -> {
// return prices[i] * 0.9;
// });
// Arrays.setAll(prices, i -> prices[i] * 0.9);
System.out.println(Arrays.toString(prices));
Student[] students = {
new Student("张三", 18, 175),
new Student("李四", 20, 163),
new Student("王五", 19, 170)
};
// Arrays.sort(students, new Comparator<Student>() {
// @Override
// public int compare(Student s1, Student s2) {
// return Double.compare(s1.getHeight(), s2.getHeight())
// }
// });
Arrays.sort(students, (s1, s2) -> Double.compare(s1.getHeight(), s2.getHeight()));
System.out.println(Arrays.toString(students));
}
}