java数组常用功能

时间:2021-04-15 15:37:04

复制数组:System.arraycopy()

public class Main{
    public static void main(String[] args){
        int[] i = new int[7];
        int[] j = new int[10];
        Arrays.fill(i, 47);
        Arrays.fill(j, 99);
        System.out.println("i = " + Arrays.toString(i));
        System.out.println("j = " + Arrays.toString(j));
        System.arraycopy(i,0,j,0,i.length);
        System.out.println("j = "+ Arrays.toString(j));
        int[] k = new int[5];
        Arrays.fill(k, 103);
        System.arraycopy(i,0,k,0,k.length);
        System.out.println("k = " + Arrays.toString(k));
        Arrays.fill(k, 103);
        System.arraycopy(k, 0 ,i,0,k.length);
        System.out.println("i = " + Arrays.toString(i));

        Integer[] u = new Integer[10];
        Integer[] v = new Integer[5];
        Arrays.fill(u,new Integer(47));
        Arrays.fill(v,new Integer(99));
        System.out.println("u = "+ Arrays.toString(u));
        System.out.println("v = "+ Arrays.toString(v));
        System.arraycopy(v, 0, u, u.length/2, v.length);
        System.out.println("u = "+ Arrays.toString(u));
    }
}

运行结果:

i = [47, 47, 47, 47, 47, 47, 47]
j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]
k = [47, 47, 47, 47, 47]
i = [103, 103, 103, 103, 103, 47, 47]
u = [47, 47, 47, 47, 47, 47, 47, 47, 47, 47]
v = [99, 99, 99, 99, 99]

u = [47, 47, 47, 47, 47, 99, 99, 99, 99, 99]

       用此方法复制,比用for循环快的多。此复制为浅复制,如果复制对象数组,那么只是复制了对象的引用,而不是对象本身的拷贝。并且,System.arraycopy()并不会自动包装和自动拆包,所以int和Integer是不能相互复制的

  数组的比较:相等的条件是元素个数相等 ,并且对应位置的元素也相等

  Arrays.equals(a1, a2)

public class Main{
    public static void main(String[] args){
      int[] a1 = new int[10];
      int[] a2 = new int[10];
      Arrays.fill(a1, 47);
      Arrays.fill(a2, 47);
      System.out.println(Arrays.equals(a1, a2));
      a2[3] = 11;
      System.out.println(Arrays.equals(a1, a2));
      String[] s1 = new String[4];
      Arrays.fill(s1, "Hi");

      String[] s2 = {new String("Hi"), new String("Hi"),
               new String("Hi"), new String("Hi")};

      System.out.println(Arrays.equals(s1, s2));
    }
}

运行结果:

true

false

true

    可以发现,s1的所有元素指向同一个对象,而s2中的对象相互独立,数组相等时基于内容的

    注:关于字符串的初始化可以参考 https://blog.csdn.net/qq_27469549/article/details/79898589

对自定义的对象进行排序

 class CollectionData<T> extends ArrayList<T> {
    public CollectionData(Generator<T> gen,int quantity){
        for(int i=0;i<quantity;i++){
            this.add(gen.next());
        }
    }
    // A generic convenience method.
    public static <T> CollectionData<T> list(Generator<T> gen,int quantity){
        return new CollectionData<T>(gen, quantity);
    }
}

interface Generator<T>{
    T next();
}

class Generated{
    public static <T> T[] array(T[] a, Generator<T> gen){
        return new CollectionData<T>(gen, a.length).toArray(a);
    }

    public static <T> T[] array(Class<T> type, Generator<T> gen, int size){
        T[] a = (T[])java.lang.reflect.Array.newInstance(type, size);
        return new CollectionData<T>(gen, size).toArray(a);
    }
}


class CompType implements Comparable<CompType> {
    int i;
    int j;
    private static int count = 1;
    public CompType(int n1, int n2){
        i = n1;
        j = n2;
    }

    public String toString(){
        String result = "[i = " + i + ", j = " + j + "]";
        if(count % 3 == 0){
            result += "\n";
        }
        return result;
    }

    public int compareTo(CompType rv){
        return (i < rv.i ? -1 : (i == rv.i ? 0 : 1));
    }
    private static Random  r = new Random(47);

    public static Generator<CompType> generator(){
        return new Generator<CompType>() {
            @Override
            public CompType next() {
                return new CompType(r.nextInt(100), r.nextInt(100));
            }
        };
    }
}



public class Main{
    public static void main(String[] args){
        CompType[] a = Generated.array(new CompType[12], CompType.generator());
        System.out.println("before sorting");
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("After sorting");
        System.out.println(Arrays.toString(a));
    }
}

运行结果:

before sorting
[[i = 58, j = 55], [i = 93, j = 61], [i = 61, j = 29], [i = 68, j = 0], [i = 22, j = 7], [i = 88, j = 28], [i = 51, j = 89], [i = 9, j = 78], [i = 98, j = 61], [i = 20, j = 58], [i = 16, j = 40], [i = 11, j = 22]]
After sorting
[[i = 9, j = 78], [i = 11, j = 22], [i = 16, j = 40], [i = 20, j = 58], [i = 22, j = 7], [i = 51, j = 89], [i = 58, j = 55], [i = 61, j = 29], [i = 68, j = 0], [i = 88, j = 28], [i = 93, j = 61], [i = 98, j = 61]]

对字符串进行排序

public class Main{
    public static void main(String[] args){
        String[] sa = {"dfs","jkm","Jkd","sdfgv","AWERR","terr","PERJMjd","QERM","vklo"};
        System.out.println("Before sort: " + Arrays.toString(sa));
        Arrays.sort(sa);
        System.out.println("After sort: " + Arrays.toString(sa));
        Arrays.sort(sa, Collections.reverseOrder());          //反序
        System.out.println("reverseOrder sort: " + Arrays.toString(sa));
    }
}

运行结果:

Before sort: [dfs, jkm, Jkd, sdfgv, AWERR, terr, PERJMjd, QERM, vklo]
After sort: [AWERR, Jkd, PERJMjd, QERM, dfs, jkm, sdfgv, terr, vklo]

reverseOrder sort: [vklo, terr, sdfgv, jkm, dfs, QERM, PERJMjd, Jkd, AWERR]

       Java标准类库中的排序算法针对正排序的特殊类型进行了优化——针对基本类型设计的“快速排序”,以及针对对象设计的“稳定归并排序”。