JAVA中的Arrays类总结

常用API

Arrays类

在这里插入图片描述

int[] list = new int[]{1,3,5,7,9,11,15,18};
        // toString()方法 返回内容
        System.out.println(Arrays.toString(list));

        // copyRange() 拷贝数组数据 包前不包后 截取部分数组
        int[] copylist =  Arrays.copyOfRange(list,0,2);
        System.out.println(Arrays.toString(copylist));

        // copyOf()  不可变长数组扩容 一般第二个参数比原数组长度要长
        int[] newlist = Arrays.copyOf(list,10);

        // setAll() 将原数组中元素做操作再存入
        // IntToDoubleFunction 匿名内部类
        // int i 的值是遍历得到的prices的索引值
        double[] prices = new double[]{16.2,15.2,41.2};
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int i) {
                double temp = prices[i]*2;
                return temp;
            }
        });

        System.out.println(Arrays.toString(prices));


        // sort()排序 默认升序
        Arrays.sort(prices);
        System.out.println(Arrays.toString(prices));

Arrays类Sort()函数 对象排序

在这里插入图片描述

让需要额外比较规则的类重新比较

方式一 让待比较的成员类实现特定接口

Person[] people = new Person[3];
       people[0] = new Person(5,"daw");
       people[1] = new Person(1,"weqw");
       people[2] = new Person(3,"3");
       Arrays.sort(people);
       System.out.println(Arrays.toString(people));
public class Person implements Comparable<Person>{
    // 重写比较规则
    // this比较者 被比较者o
    // 若this比o大返回正整数 若this比o小返回负整数
    // 若this和o相同 返回0
    @Override
    public int compareTo(Person o) {
        if(this.id > o.id){
            return 1;
        }
        else if(this.id < o.id){
            return -1;
        }else{
            return 0;}
    }

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Person(){

    }
    }

方式二 将比较器对象作为Sort()函数的形参

按学生ID排序

public class Test1 {
    public static void main(String[] args) {
       Person[] people = new Person[3];
       people[0] = new Person(5,"daw");
       people[1] = new Person(1,"weqw");
       people[2] = new Person(3,"3");
       Arrays.sort(people, new Comparator<Person>() {
           @Override
           public int compare(Person person, Person t1) {
               return  (person.getId()-person.getId());
           }
       });
       System.out.println(Arrays.toString(people));
    }
}

按double类型的身高排序

使用包装类Double自带的比较函数

public class Test1 {
    public static void main(String[] args) {
       Person[] people = new Person[3];
       people[0] = new Person(5,"daw",155.0);
       people[1] = new Person(1,"weqw",155.1);
       people[2] = new Person(3,"3",195.0);
       Arrays.sort(people, new Comparator<Person>() {
           @Override
           public int compare(Person person, Person t1) {
               return Double.compare(person.getHeight(),t1.getHeight()) ;
           }
       });

        for (int i = 0; i < people.length; i++) {
            System.out.println("this is " + (i+1) + "th person named:" + people[i].getId()+"and his or her height is"+people[i].getHeight());

        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

巴巴_羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值