Arrays

Arrays

用来操作数组的一个工具类

Arrays类提供的常见方法

方法名说明
public static String toString(类型[] arr)返回数组内容
public static int[] copyOfRange(类型[] arr, 起始索引, 结束索引)拷贝数组(指定范围)
public static copyOf(类型[] arr, int newLength)拷贝数组
public static setAll(double[] array, IntToDoubleFunction generator)把数组中的原数据改为新数据
public static void sort(类型[] arr)对数组进行排序,默认是升序排序

案例演示

public class ArraysTest {
    public static void main(String[] args) {
        //public static String toString(类型[] arr):返回数组的内容
        int[] IntArr = new int[] {10, 20, 30, 40, 50};
        System.out.println(Arrays.toString(IntArr));    //[10, 20, 30, 40, 50]

        //public static int[] copyOfRange(类型[] arr, 起始索引, 结束索引):拷贝数组(指定范围,包前不包后)
        System.out.println("-------------");
        int[] IntArr2 = Arrays.copyOfRange(IntArr, 1, 4);
        System.out.println(Arrays.toString(IntArr2));   //[20, 30, 40]

        //public static copyOf(类型[] arr, int newLength):拷贝数组,可以指定新数组的长度
        System.out.println("-----------------");
        int[] IntArr3 = Arrays.copyOf(IntArr, 10);
        System.out.println(Arrays.toString(IntArr3));   //[10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
        System.out.println(IntArr3.length);             //10

        //public static setAll(double[] array, IntToDoubleFunction generator):把数组中的原数据改为新数据又存进去
        System.out.println("--------------------");
        double[] numbers = new double[] {12.12, 55.6, 33.4, 100};
        //将数组中所有元素乘上100后又存进去
        Arrays.setAll(numbers, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                return numbers[value] * 100;
            }
        });
        System.out.println(Arrays.toString(numbers));   //[1212.0, 5560.0, 3340.0, 10000.0]

        //public static void sort(类型[] arr):对数组进行排序(默认是升序排序)
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));   //[1212.0, 3340.0, 5560.0, 10000.0]

    }
}

对于数组中存放对象的情况下,有两种排序方式:

一、让该对象的类实现Comparable(比较规则)接口,然后重写compareTo()方法,自己来制定比较规则

使用以下sort方法,创建Comparator比较器接口的匿名内部类对象,然后自己指定比较规则:

public static  void sort(T[] arr, Comparator<? super T> C) 对数组进行排序(支持自定义排序规则)

案例演示

Student类

public class Student implements Comparable<Student> {
    private String name;
    private int age;
    private double score;

    public Student() {
    }

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    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 getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "{" +
                name +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student student) {
        //左边对象大于右边(this.xx > student.xx),返回正整数
        //左边对象小于右边(this.xx < student.xx),返回负整数
        //左边对象等于右边(this.xx == student.xx),返回0
        if (this.score > student.score)
            return 1;
        else if (this.score < student.score)
            return -1;
        return 0;
    }
}

测试类ArraysTest2

public class ArraysTest2 {
    public static void main(String[] args) {
        Student[] students = new Student[4];
        students[0] = new Student("王亮", 17, 85.5);
        students[1] = new Student("李雷", 16, 95);
        students[2] = new Student("韩梅梅", 16, 96);
        students[3] = new Student("李华", 18, 73);

        //类实现Comparable接口并重写compareTo()方法
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));

        //Comparator比较器对象(用来制定对象的比较规则)
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println(Arrays.toString(students));
    }
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离歌慢饮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值