Java的Arrays类
1.返回数组的内容
int[]arr1={1,2,3,4};String s = Arrays.toString(arr1);System.out.println(s);
2.拷贝数组(指定范围,包前不包后)
int[]arr1={1,2,3,4};String s = Arrays.toString(arr1);System.out.println(s);int[] copy = Arrays.copyOfRange(arr1, 1, 4);System.out.println(copy);//直接输出数组//返回的是地址System.out.println(Arrays.toString(copy));
3.指定新数组的长度----用于扩容数组
int[]arr1={1,2,3,4};String s = Arrays.toString(arr1);System.out.println(s);int[] copy = Arrays.copyOfRange(arr1, 1, 4);System.out.println(copy);//直接输出数组//返回的是地址System.out.println(Arrays.toString(copy));int[] copy1 = Arrays.copyOf(arr1, 10);System.out.println(Arrays.toString(copy1));
4.把数组中的原数据改为新数据存进去
double[]price={11,22,13};Arrays.setAll(price, new IntToDoubleFunction() {@Overridepublic double applyAsDouble(int value) {return price[value]*0.8;}});System.out.println(Arrays.toString(price));
5.对数组进行排序(默认是升序排序)
int[]arr1={2,1,3,4};Arrays.sort(arr1);System.out.println(Arrays.toString(arr1));
public class Student implements Comparator<Student> {String name;int year;String tel;double height;public Student() {}public Student(String name, double height, String tel, int year) {this.name = name;this.height = height;this.tel = tel;this.year = year;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}Student s1 = new Student("张三", 178.2, "1223434", 12);Student s2 = new Student("李四", 168.0, "1345656", 42);Student s3 = new Student("小明", 170.5, "1451345", 30);Student s4 = new Student("小王", 175.1, "1456112", 28);Student[] students = new Student[4];students[0]=s1;students[1]=s2;students[2]=s3;students[3]=s4;//第一种方法Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return Double.compare(o1.getHeight(), o2.getHeight());}});System.out.println(Arrays.toString(students));public class Student implements Comparator<Student> {String name;int year;String tel;double height;public Student() {}public Student(String name, double height, String tel, int year) {this.name = name;this.height = height;this.tel = tel;this.year = year;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", year=" + year +", tel='" + tel + '\'' +", height=" + height +'}';}@Overridepublic int compare(Student o1, Student o2) {return o1.year-o2.year;}}Student s1 = new Student("张三", 178.2, "1223434", 12);Student s2 = new Student("李四", 168.0, "1345656", 42);Student s3 = new Student("小明", 170.5, "1451345", 30);Student s4 = new Student("小王", 175.1, "1456112", 28);Student[] students = new Student[4];students[0]=s1;students[1]=s2;students[2]=s3;students[3]=s4;//第二种方法System.out.println(Arrays.toString(students));}