2011-10-23 127 views

回答

6

按照你的逻辑

int i = 0; 
for(ArrayList<Integer> a1 : outer){ 
    int j=0; 
    for(Integer k : a1){ 
     yourarray[i][j++] = k; 
    } 
    i++; 
} 
+0

任何投票的理由?如果我至少错了,请提供一个理由,以便我可以学习。 –

+1

嗨,很抱歉,您从未重置过“j”。这将产生幻想指数超出界限例外。而在Java中,你应该写变量名小写。 –

+0

我改变了它;) –

0

我想你需要循环的两个层面做到这一点。

+0

有没有违约util的方法获取的ArrayList的大小为i和j,分别 递增i和j? –

+0

否:toArray无法使用,因为在内部列表之一上调用它会给出与int []不同的Integer []。 –

1

我同意莫里斯。

要将List<Integer>转换为int[],有一个nice answer here,您可能会觉得有用。

另请注意,Apache Commons有一个ArrayUtils class,它有一个方法toPrimitive(),它完全适用于一个数组级别。

编辑(见下面注释):

msandiford还提到guavaInts类,它提供一个静态int[] toArray(Collection<Integer> collection)方法的存在。

+0

已添加Apache Commons ArrayUtils类的完整性 –

+0

Google guava的com.google.common.primitives.Ints具有'toArray(Collection )'方法,可能比'toPrimitive(foo.toArray())'效率更高。 http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/primitives/Ints.html#toArray(java.util.Collection) – msandiford

+0

@msandiford感谢您的链接,我更新了答案。 –

相关问题