gpt4 book ai didi

Java - 将 ArrayList< int[] > 转换为 int[][]

转载 作者:行者123 更新时间:2023-11-29 03:29:14 24 4
gpt4 key购买 nike

我有一个 ArrayList< int[] > 并想将其转换为 int[][]。结果数组的每个条目都具有相同的 int[],即使它们是不同的(是的,我已经检查过了!)。我在这里错过了一些简单的东西吗? int[][] 中数组的长度由 arr.length 给出

int[][] vals = new int[ list.size() ][ arr.length ];
list.toArray( vals );

编辑:我已经意识到代码工作正常,所以这里是出现问题的代码的更大样本。 permute() 按照它说的去做,并在新的排列完成时排列给定数组中的整数返回 true。

List< int[] > list = new ArrayList<>();
do {
list.add( vals );
} while ( permute( vals ) ); // I print vals here and the permutations are all unique each time
int[][] permutations = new int[ list.size() ][];
list.toArray( permutations );
// If I print permutations now, all the arrays inside it are the same

最佳答案

int[][] vals = new int[ list.size() ][ arr.length ];

for(int i = 0; i < list.size(); i++) {
vals[i] = list.get(i);
}

关于Java - 将 ArrayList< int[] > 转换为 int[][],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19259539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com