gpt4 book ai didi

java - 在java中将一个数组复制到另一个数组中。数组存储异常

转载 作者:行者123 更新时间:2023-12-02 10:30:40 26 4
gpt4 key购买 nike

我试图在我拥有的数组的末尾复制一个新数组,但它一直在 System.arraycopy 中显示 ArrayStoreException,我不知道为什么,它应该有空间和所有内容。

这是代码:

objectzombie=zombieParser(result); /*give a 4 object array*/
GameObjectList arrayt= new GameObjectList(objectzombie.size()+this.objectlist.size());

System.arraycopy(objectzombie, 0, arrayt, 0, objectzombie.size());
System.arraycopy(this.objectlist, 0, arrayt, arrayt.size(),
this.objectlist.size());
this.objectlist=arrayt;}

感谢您的帮助;

编辑-------------

看起来,如果我没有原始数组,我就无法使用 arraycopy,那么我如何组合我的两个列表?我不知道我该怎么做。

最佳答案

根据Java SE API documentationSystem.arraycopy参数是:

  • src - 源数组。
  • srcPos - 源数组中的起始位置。
  • dest - 目标数组。
  • destPos - 目标数据中的起始位置。
  • length - 要复制的数组元素的数量。

method description ,还有:

Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

  • The src argument refers to an object that is not an array.

  • The dest argument refers to an object that is not an array.

  • The src argument and dest argument refer to arrays whose component types are different primitive types.

  • The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.

  • The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.

srcdest数组,而您尝试复制到 GameObjectList 类型的目标对象,这是不匹配的 => ArrayStoreException

关于java - 在java中将一个数组复制到另一个数组中。数组存储异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53632528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com