Java Array Copy arrayCopy(T[] x)

Here you can find the source of arrayCopy(T[] x)

Description

array Copy

License

Open Source License

Declaration

public static <T> T[] arrayCopy(T[] x) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static <T> T[] arrayCopy(T[] x) {
        return arrayCopy(x, true);
    }/*from   w w w . ja  v a 2 s  .  c  o m*/

    public static <T> T[] arrayCopy(T[] x, boolean notNull) {
        int z = 0;
        T[] l = x.clone();
        for (int i = 0; i < x.length; i++) {
            T y = x[i];
            l[i] = null;
            if (!notNull || y != null) {
                l[z] = y;
                z++;
            }
        }
        T[] n = Arrays.copyOf(l, z);
        return n;
    }

    public static int[] arrayCopy(int[] x) {
        return arrayCopy(x, false);
    }

    public static int[] arrayCopy(int[] x, boolean notZero) {
        int z = 0;
        int[] l = new int[x.length + 1];
        for (int i = 0; i < x.length; i++) {
            int y = x[i];
            if (!notZero || y != 0) {
                l[z] = y;
                z++;
            }
        }
        int[] n = new int[z];
        System.arraycopy(l, 0, n, 0, z);
        return n;
    }

    public static byte[] arrayCopy(byte[] x) {
        return arrayCopy(x, false);
    }

    public static byte[] arrayCopy(byte[] x, boolean notZero) {
        int z = 0;
        byte[] l = new byte[x.length + 1];
        for (int i = 0; i < x.length; i++) {
            byte y = x[i];
            if (!notZero || y != 0) {
                l[z] = y;
                z++;
            }
        }
        byte[] n = new byte[z];
        System.arraycopy(l, 0, n, 0, z);
        return n;
    }

    public static char[] arrayCopy(char[] x) {
        return arrayCopy(x, false);
    }

    public static char[] arrayCopy(char[] x, boolean notZero) {
        int z = 0;
        char[] l = new char[x.length + 1];
        for (int i = 0; i < x.length; i++) {
            char y = x[i];
            if (!notZero || y != 0) {
                l[z] = y;
                z++;
            }
        }
        char[] n = new char[z];
        System.arraycopy(l, 0, n, 0, z);
        return n;
    }

    public static <T> T[] arrayCopy(T[] x, int size) {
        int z = 0;
        T[] l = x.clone();
        for (int i = 0; i < x.length; i++) {
            T y = x[i];
            l[i] = null;
            if (y != null) {
                l[z] = y;
                z++;
            }
        }
        T[] n = Arrays.copyOf(l, size);
        return n;
    }
}

Related

  1. arrayCopy(String[][] src, int src_position, String[][] dst, int dst_position, int length)
  2. arraycopy(T[] a)
  3. arrayCopy(T[] objs)
  4. arraycopy(T[] src, int srcPos, T[] dest, int destPos, int length)
  5. arraycopy(T[] src, int srcPos, T[] dst, int len)
  6. arrayCopyAndAddEntry(T[] base, T extra)
  7. arraycopyAndInsertInt(final int[] src, final int idx, final int value)
  8. arrayCopyToNull(T[] source, int sourceOffset, T[] destination, int destinationOffset)
  9. arrayCopyWithRemoval(Object[] src, Object[] dst, int idxToRemove)