Arrays的常用方法
方法代码示例
package com.zz.arrays;
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;
public class Test {
public static void main(String args[]){
//1.public static String toString(类型[] arr):返回数组的内容
int[] arr = {10, 123, 34, 34};
System.out.println(Arrays.toString(arr));
//2.public static 类型[] copyOf(类型[] arr, int newLength):拷贝数组,可以指定数组的长度
int arr2[] = Arrays.copyOf(arr, 3);
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 serAll(double[] array, IntToDoubleFunction generator),
* 把数组中的原数组改为新数据存进去
*/
double[] prices = {89, 57, 3997};
//把所有的价格都打八折,然后存进去
Arrays.setAll(prices, new IntToDoubleFunction() {
@Override
public double applyAsDouble(int value) {
//value = 0 1 2
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));
}
}
如果数组中存储的是对象,如何排序?
主函数代码:
package com.zz.arrays;
import java.util.Arrays;
import java.util.Comparator;
public class sortTest {
public static void main(String args[]){
Student[] students = new Student[4];
students[0] = new Student("林黛玉", 167.7, 20);
students[1] = new Student("鲁智深", 189, 34);
students[2] = new Student("孙悟空", 167.8, 23);
students[3] = new Student("红孩儿", 150, 3);
//方法一:實現Comparable接口,重寫compareTo方法
// Arrays.sort(students);
// System.out.println(Arrays.toString(students));shixian
/**
* 方法二:public static<T> void sort(T[] arr, Comparator<? super T > c)
* 参数1:需要排序的数组
* 参数2:Comparable比较器对象(用来制定对象的比较规则)
*/
Arrays.sort(students, new Comparator <Student>() {
@Override
public int compare(Student o1, Student o2) {
/**
* o1 大于 o2 则返回正整数
* o1 小于 o2 则返回负整数
* o1 等于 o2 则返回0
*/
//写法1
// if(o1.getHeight() > o2.getHeight()){
// return 1;
// }else if(o1.getHeight() < o2.getHeight()){
// return -1;
// }
// return 0;
//写法2
//return Double.compare(o1.getHeight(),o2.getHeight());升序
return Double.compare(o2.getHeight(),o1.getHeight());//降序
}
});
System.out.println(Arrays.toString(students));
}
}
Student类
package com.zz.arrays;
public class Student implements Comparable<Student>{
private String name;
private double height;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, double height, int age) {
this.name = name;
this.height = height;
this.age = age;
}
@Override
public int compareTo(Student o) {
/**
* 这里是this和o比较
* 约定1:如果this 大于 o 则返回正整数
* 约定2,:如果this 小于 o 则返回负整数
* 约定2,:如果this 等于 o 则返回0
*/
//方法1:按照年龄排序
// if(this.age > o.age){
// return 1;
// }else if(this.age < o.age){
// return -1;
// }
// return 0;
//方法2
return this.age - o.age;
}
@Override
public String toString() {
return "Student{" + "name='" + name + '\'' + ", height=" + height + ", age=" + age + '}';
}
}