java将LinkedList的ArrayList清空回数组
我正在尝试将LinkedList的ArrayList中的元素移回数组中。我发现一个元素未找到异常
ArrayList<LinkedList<Integer>> stacks = new ArrayList<LinkedList<Integer>>(3);
int[] arr = new arr[5];
stacks.get(0).add(22);
stacks.get(0).add(1);
stacks.get(0).add(7);
stacks.get(1).add(111);
stacks.get(2).add(123);
for (int i = 0; i < arr.length; i++)
{
while (!stacks.isEmpty())
{
arr[i++] = stacks.get(i).remove();
}
}
我会在假设while不为空的情况下进行操作,以解释这一点。我很好奇为什么它不能成功地复制内容
# 1 楼答案
这样就行了。本例使用
ArrayLists
,但它也将与LinkedLists
或任何实现List
接口的东西一起使用创建列表列表
并转换成
int array
印刷品
# 2 楼答案
代码中有几个问题
您没有将
LinkedList
添加到ArrayList
。您需要先添加LinkedList
的,然后再将项目添加到stacks
你的循环完全错了,我已经重写了