简体   繁体   English

如何转换ArrayList <ArrayList<Integer> &gt;转换为int [] []?

[英]how to convert ArrayList<ArrayList<Integer>> to int[][]?

我有一个旧函数,我无法更改谁想要参数int [] []是否可以将ArrayList<ArrayList<Integer>>映射到int[][]

Get the size of the ArrayLists into i and j ,respectively Increment i and j according to your logic 将ArrayList的大小分别取为i和j,根据您的逻辑分别递增i和j

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

I agree with Maurice. 我同意莫里斯。

To convert List<Integer> to int[] , there's a nice answer here that you may find usefull. 要将List<Integer>转换为int[] ,这里有一个不错的答案 ,您可能会觉得有用。

Also note that Apache Commons has a ArrayUtils class , which has a method toPrimitive() that does exactly this for one array level. 还要注意,Apache Commons有一个ArrayUtils类该类具有toPrimitive()方法,该方法在一个数组级别上完全做到这一点。

Edit (see comments below): 编辑(请参见下面的评论):

msandiford also mentions the existence of guava Ints class that provides a static int[] toArray(Collection<Integer> collection) method. msandiford还提到了guava Ints类的存在,该类提供了一个静态int[] toArray(Collection<Integer> collection)方法。

我想您将需要两个级别的循环来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM