日期:2014-05-20  浏览次数:20872 次

copyOf为什么过用不成功?
import java.util.*;

public class Test
{
public static void main(String[] args)
{
int[] array = {1, 2, 3};
int[] b = Arrays.copyOf(array, 0, 2);
for(int e: b)
{
System.out.println(e);
}

}
}


错误提示为
对于copyOf(int[],int,int), 找不到合适的方法
             int[] b = Arrays.copyOf(array, 0, 2);

我看到书上写着公式为
static type copyOf(type[] a, int start, int end)
为什么我的不成功呢?

------解决方案--------------------
  /**
     * Copies the specified array, truncating or padding with zeros (if necessary)
     * so the copy has the specified length.  For all indices that are
     * valid in both the original array and the copy, the two arrays will
     * contain identical values.  For any indices that are valid in the
     * copy but not the original, the copy will contain <tt>0</tt>.
     * Such indices will exist if and only if the specified length
     * is greater than that of the original array.
     *
     * @param original the array to be copied
     * @param newLength the length of the copy to be returned
     * @return a copy of the original array, truncated or padded with zeros
     *     to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException if <tt>original</tt> is null
     * @since 1.6
     */
    public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

参数个数。
------解决方案--------------------
Arrays.copyOf 没有  3个参数的方法。
------解决方案--------------------