copyOf方法未定义类型数组 [英] copyOf method undefined for the type Arrays

查看:328
本文介绍了copyOf方法未定义类型数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

elementData = Arrays.copyOf(elementData, newCapacity);

产生错误:


方法copyOf(Object [],int)未定义为类型数组

The method copyOf(Object[], int) is undefined for the type Arrays

这不是我的问题家庭电脑,但在我学校的它给出了上面的错误。

This was not a problem on my home computer, but at my school's it gives the error above. I'm guessing it's running an older JRE version - any workaround?

推荐答案

Arrays.copyOf()

Arrays.copyOf() was introduced in 1.6.

你需要创建一个你需要的新数组,并将旧数组的内容复制到

You'd need to create a new array of the size you need and copy the contents of the old array into it.

From: http://www.source-code.biz/snippets/java/3.htm

/**
* Reallocates an array with a new size, and copies the contents
* of the old array to the new array.
* @param oldArray  the old array, to be reallocated.
* @param newSize   the new array size.
* @return          A new array with the same contents.
*/
private static Object resizeArray (Object oldArray, int newSize) {
   int oldSize = java.lang.reflect.Array.getLength(oldArray);
   Class elementType = oldArray.getClass().getComponentType();
   Object newArray = java.lang.reflect.Array.newInstance(
         elementType,newSize);
   int preserveLength = Math.min(oldSize,newSize);
   if (preserveLength > 0)
      System.arraycopy (oldArray,0,newArray,0,preserveLength);

   return newArray; 
}

这篇关于copyOf方法未定义类型数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆