Arrays针对数组使用提供了一些实用方法

  • A+
所属分类:编程茶楼


共计 2314 个字符,预计需要花费 6 分钟才能阅读完成。

复制数组

参数:源数组,从源数组哪个索引开始复制的偏移量,从目标数组哪个索引开始复制的偏移量,需要复制的元素个数

Tips:此方法不会执行自动包装盒拆包,两个数组需明确类型

class CopyingArrays {
  public static void main(String[] args) {
      int[] arr1 = new int[5];
      int[] arr2 = new int[8];
      Arrays.fill(arr1, 11); // 填充数组1
      Arrays.fill(arr2, 22); // 填充数组2
      System.out.println("arr1 = " + Arrays.toString(arr1));
      System.out.println("arr2 = " + Arrays.toString(arr2));

      System.arraycopy(arr1, 0, arr2, 0, arr1.length);
      System.out.println("arr2 = " + Arrays.toString(arr2));
      int[] arr3 = new int[3];
      System.out.println("arr3 = " + Arrays.toString(arr3));
      System.arraycopy(arr2, 0, arr3, 0, arr3.length);
      System.out.println("arr3 = " + Arrays.toString(arr3));
  }
  /**
   * arr1 = [11, 11, 11, 11, 11]
   * arr2 = [22, 22, 22, 22, 22, 22, 22, 22]
   * arr2 = [11, 11, 11, 11, 11, 22, 22, 22]
   * arr3 = [0, 0, 0]
   * arr3 = [11, 11, 11]
   */
}

数组的比较

数组相等的条件是元素个数必须相等,并且对应位置的元素必须相等
class CompType implements Comparable<CompType> {
    private int title;
    private int name;

    public int getTitle() {
        return title;
    }

    public void setTitle(int title) {
        this.title = title;
    }

    public int getName() {
        return name;
    }

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

    public CompType() {
    }

    private CompType(int title, int name) {
        this.title = title;
        this.name = name;
    }

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

    @Override
    public int compareTo(CompType o) {
        return Integer.compare(o.title, title); // DESC降序
    }

    public static void main(String[] args) {
        CompType[] compTypes = {
                new CompType(2, 4),
                new CompType(1, 29),
                new CompType(4, 2),
                new CompType(48, 1)
        };
        System.out.println("Before sort:" + Arrays.toString(compTypes));
        Arrays.sort(compTypes); // 实现Comparable接口
        System.out.println("After Comparable sort:" + Arrays.toString(compTypes));

        Arrays.sort(compTypes, Comparator.comparingInt(o -> o.name)); // AES升序,name
        System.out.println("After Comparator sort:" + Arrays.toString(compTypes));

        System.out.println("binarySearch:" + Arrays.binarySearch(compTypes, new CompType(2, 4), Comparator.comparingInt(o -> o.name)));
    }

    /**
     * Before sort:[CompType{title=2, name=4}, CompType{title=1, name=29}, CompType{title=4, name=2}, CompType{title=48, name=1}]
     * After sort:[CompType{title=48, name=1}, CompType{title=4, name=2}, CompType{title=2, name=4}, CompType{title=1, name=29}]
     * After Comparator sort:[CompType{title=48, name=1}, CompType{title=4, name=2}, CompType{title=2, name=4}, CompType{title=1, name=29}]
     * binarySearch:2
     */
}

数组排序

该对象实现Comparable接口,Arrays.sort(arr1)
自定义Comparator比较器,Arrays.sort(arr1,比较器)
Tip:同上

在已排序的数组中查找

Tip:数组已经排好序,同上

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: