ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException

本文介绍了System.arraycopy获取java.lang.ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.arraycopy获取ava.lang.ArrayIndexOutOfBoundsException ..我正在尝试将数据从一个数组复制到下一个数组。但是我遇到了一个例外

System.arraycopy getting ava.lang.ArrayIndexOutOfBoundsException.. I am trying to copy data from one array to the next. but I am getting a exception

private String array[] = { "NO DATA YET" };


 private void setListData()
    {
        String array2[] = { "Iphone", "Tutorials", "Gallery", "Android",    "item 1", "item 2", "item3", "item 4" };
        System.arraycopy(array2, 0, array, 0, array2.length);
    }


推荐答案

您正在尝试复制长度为1的数组中有8个项目。您不能这样做。

You're trying to copy 8 items into an array of length 1. You can't do that.

来自:


  • srcPos参数为负。

  • destPos参数为负。

  • length参数为负。

  • srcPos + length大于源数组的长度src.length。

  • destPos + length大于目标数组的长度dest.length

  • The srcPos argument is negative.
  • The destPos argument is negative.
  • The length argument is negative.
  • srcPos+length is greater than src.length, the length of the source array.
  • destPos+length is greater than dest.length, the length of the destination array.

在这种情况下, destPos +长度为8 ,并且 dest.length 为1,因此抛出异常

In this case, destPos + length is 8, and dest.length is 1, hence the exception is being thrown.

请注意,Java中的数组具有固定的长度。如果要使用可扩展容器,请查看 ArrayList

Note that arrays in Java have a fixed length. If you want an expandable container, look at ArrayList.

这篇关于System.arraycopy获取java.lang.ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 20:11