java_Arrays

Arrays

常用方法:
toString
sort排序
binarySearch 二叉查找 传入的数组要是有序的(升序)
copyOf (array,array.length) >数组长度 后面追加null <0 报异常
fill 数组元素填充 全部填充
equals 比较2数组元素
asList() 将数据转成List集合 编译类型List 运行类型 java.util.Arrays$ArrayList

		Integer[] integers = {1,3,2,4,6};
        Arrays.sort(integers);//默认排序
        System.out.println(Arrays.toString(integers));//[1, 2, 3, 4, 6]
		
		 Integer[] integers = {1,3,2,4,6};
        //定制排序 数组 实现Comparator接口的匿名内部类
        /*
        while (left < right) {
            int mid = (left + right) >>> 1;
            if (c.compare(pivot, a[mid]) < 0) //动态绑定,执行我们重写的copare方法
                right = mid;
            else
                left = mid + 1;
        }
        assert left == right;
        */
       
        Arrays.sort(integers, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer o11 = (Integer) o1;
                Integer o21 = (Integer) o2;
                return o21 -o11; //
            }
        });
        System.out.println(Arrays.toString(integers));

定制排序的自我实现

public static void main(String[] args) {
      int[] a ={1,7,6,5,2,8};
//      bubble(a);
      bubble02(a, new Comparator() {
          @Override
          public int compare(Object o1, Object o2) {
              Integer o11 = (Integer) o1;
              Integer o21 = (Integer) o2;
              return o21 -o11;
          }
      });
        System.out.println(Arrays.toString(a));
    }

    //冒泡排序
    public static void bubble(int[] arr){ //小到大
        int temp =0;
        for (int i = 0; i < arr.length -1; i++) {
            for(int j =0; j< arr.length-1-i;j++){
                if(arr[j] >arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    public static void bubble02(int[] arr, Comparator c){
        int temp =0;
        for (int i = 0; i < arr.length -1; i++) {
            for(int j =0; j< arr.length-1-i;j++){
                if(c.compare(arr[j],arr[j+1])>0){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
                //else 不操作
            }
        }
    }

随机练习
书名数组按照价格 排序 按照书名长度排序

    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("卡拉马佐夫",70);
        books[1] = new Book("群魔",30);
        books[2] = new Book("罪与罚",40);
        books[3] = new Book("献给阿尔吉侬的花束",20);

//        Arrays.sort(books, new Comparator<Book>() {
//            @Override
//            public int compare(Book o1, Book o2) {
//                int price = (int)o1.getPrice();
//                int price1 =(int) o2.getPrice();
//                return price - price1;
//            }
//        });
        //价格排序
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book o11 = (Book) o1;
                Book o21 = (Book) o2;
                int price = (int)o11.getPrice();
                int price1 =(int) o21.getPrice();
                return price - price1;
            }
        });
        //书名长度排序
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book o11 = (Book) o1;
                Book o21 = (Book) o2;
                return o11.getName().length() - o21.getName().length();
            }
        });
        System.out.println(Arrays.toString(books));
    }
    
}

class Book{
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值