Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取另一个arraylist中的arraylist实例_Java_Arraylist_Indexoutofboundsexception - Fatal编程技术网

Java 如何获取另一个arraylist中的arraylist实例

Java 如何获取另一个arraylist中的arraylist实例,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,我在arraylist(嵌套arraylist)中有一个arraylist,如下所示 但这给了我一个错误 线程“main”java.lang.IndexOutOfBoundsException中出现异常:索引:0,大小:0 如何解决这个问题以及为什么会发生这种情况 谢谢这里的问题似乎是列表大小为0,而您仍在尝试访问位置0处的元素 如果不使用循环/迭代器,或者不进行比较大小和给定索引的检查,就不应该尝试直接访问集合中的元素。您的代码应该类似于 ArrayList<Integer> te

我在arraylist(嵌套arraylist)中有一个arraylist,如下所示

但这给了我一个错误

线程“main”java.lang.IndexOutOfBoundsException中出现异常:索引:0,大小:0

如何解决这个问题以及为什么会发生这种情况


谢谢

这里的问题似乎是列表大小为0,而您仍在尝试访问位置0处的元素

如果不使用循环/迭代器,或者不进行比较大小和给定索引的检查,就不应该尝试直接访问集合中的元素。您的代码应该类似于

ArrayList<Integer> tempJSONObjectAL= null;
if(indexOfJSONObject.size() > givenIndex)
    tempJSONObjectAL = (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);
ArrayList tempJSONObjectAL=null;
if(indexOfJSONObject.size()>givenIndex)
tempJSONObjectAL=(ArrayList)indexOfJSONObject.get(givenIndex);

这里的问题似乎是列表大小为0,而您仍在尝试访问位置0处的元素

如果不使用循环/迭代器,或者不进行比较大小和给定索引的检查,就不应该尝试直接访问集合中的元素。您的代码应该类似于

ArrayList<Integer> tempJSONObjectAL= null;
if(indexOfJSONObject.size() > givenIndex)
    tempJSONObjectAL = (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);
ArrayList tempJSONObjectAL=null;
if(indexOfJSONObject.size()>givenIndex)
tempJSONObjectAL=(ArrayList)indexOfJSONObject.get(givenIndex);

请尝试以下代码:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> tempJSONObjectAL=new ArrayList<Integer>();

        for(ArrayList<Integer> list:indexOfJSONObject)
        {
            tempJSONObjectAL.add(list.get(index));

        }
ArrayList indexOfJSONObject=new ArrayList();
ArrayList tempJSONObject=新建ArrayList();
for(ArrayList列表:indexOfJSONObject)
{
tempJSONObjectAL.add(list.get(index));
}

请尝试以下代码:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> tempJSONObjectAL=new ArrayList<Integer>();

        for(ArrayList<Integer> list:indexOfJSONObject)
        {
            tempJSONObjectAL.add(list.get(index));

        }
ArrayList indexOfJSONObject=new ArrayList();
ArrayList tempJSONObject=新建ArrayList();
for(ArrayList列表:indexOfJSONObject)
{
tempJSONObjectAL.add(list.get(index));
}
原因这很简单。此错误是因为
indexOfJSONObject
是一个本身包含ArrayList的ArrayList。但是在indexOfJSONObject中没有任何ArrayList。
您最初是从indexOfJSONObject获取ArrayList,而它内部没有ArrayList实例化。
您需要将ArrayList的新实例化添加到indexOfJSONObject,然后使用它

通过添加一个特定的语句,可以解决这个问题。只需检查下面的代码:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();

//This line of code is required in your case
indexOfJSONObject.add(new ArrayList<Integer>());

ArrayList<Integer> tempJSONObjectAL= (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);

tempJSONObjectAL.add(value); 
ArrayList indexOfJSONObject=new ArrayList();
//这行代码在您的案例中是必需的
add(newarraylist());
ArrayList tempJSonObject=(ArrayList)indexOfJSONObject.get(givenIndex);
添加(值);
原因这很简单。此错误是因为
indexOfJSONObject
是一个本身包含ArrayList的ArrayList。但是在indexOfJSONObject中没有任何ArrayList。
您最初是从indexOfJSONObject获取ArrayList,而它内部没有ArrayList实例化。
您需要将ArrayList的新实例化添加到indexOfJSONObject,然后使用它

通过添加一个特定的语句,可以解决这个问题。只需检查下面的代码:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();

//This line of code is required in your case
indexOfJSONObject.add(new ArrayList<Integer>());

ArrayList<Integer> tempJSONObjectAL= (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);

tempJSONObjectAL.add(value); 
ArrayList indexOfJSONObject=new ArrayList();
//这行代码在您的案例中是必需的
add(newarraylist());
ArrayList tempJSonObject=(ArrayList)indexOfJSONObject.get(givenIndex);
添加(值);

不难发现错误:您希望元素位于索引0处,因此,第一个但列表是空的:size=0您显然没有为indexOfJSONObject的发生提供任何数据,因为
indexOfJSONObject
不包含任何项目,并且根据文档规范调用
get
空列表会导致
IndexOutOfBoundsException
indexOfJSONObject在您调用时为空我猜你基本上在arrayList的第一个位置没有任何东西:-)这就是你得到错误IOOBE的原因。因为向arraylist添加空arraylist没有任何意义-因为第一个元素将是空对象:-)但是,如果先新建arraylist,然后再添加新arraylist,事情将按预期进行:-)。此外,使用
Map
可能会更方便、更可读。不难发现错误:您希望元素位于索引0处,因此,第一个但列表是空的:size=0您显然没有为indexOfJSONObject的发生提供任何数据,因为
indexOfJSONObject
不包含任何项目,并且根据文档规范调用
get
空列表会导致
IndexOutOfBoundsException
indexOfJSONObject在您调用时为空我猜你基本上在arrayList的第一个位置没有任何东西:-)这就是你得到错误IOOBE的原因。由于将空arraylist添加到arraylist没有任何意义-因为第一个元素将是空对象:-),但是,如果先新建arraylist,然后再添加新arraylist,事情将按预期进行:-)。此外,使用
映射可能更方便、更可读