怎么把2个长度不同的数组COPY到一个数组中。2个数组长度不同,类型相同。谢谢 大侠们~~~

解决方案 »

  1.   

    public static Object[] copy (Object[] arr1, Object[] arr2) {
    if (arr1 == null)
    throw new IllegalArgumentException("arr1 is null");
    if (arr2 == null)
    throw new IllegalArgumentException("arr2 is null"); Object[] result = new Object[arr1.length + arr2.length];
    System.arraycopy(arr1, 0, result, 0, arr1.length);
    System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
    return result;
    }
      

  2.   


    int a[] = new int[4];
    int b[] = {1};
    int c[] = {2,3,4};
    System.arraycopy(b, 0, a, 0, b.length);
    System.arraycopy(c, 0, a, b.length, c.length);
    System.out.println(Arrays.toString(a));
      

  3.   

    更好用的
    public static <T> T[] copy (T[] arr1, T[] arr2) {
    if (arr1 == null)
    throw new IllegalArgumentException("arr1 is null");
    if (arr2 == null)
    throw new IllegalArgumentException("arr2 is null"); T[] result = (T[]) Array.newInstance(arr1.getClass().getComponentType(), arr1.length + arr2.length);
    System.arraycopy(arr1, 0, result, 0, arr1.length);
    System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
    return result;
    }
      

  4.   

    如果上面的看不懂,就直接用for循环也行