我对迭代多维 ArrayList 感到困惑。 我看到了一些有关它的主题,它们看起来很复杂。具体而言,接受的 在这里回答: How to iterate through two dimensional ArrayList using iterator? 可以像这个方法一样吗?它迭代 arrayList 并将值复制到数组:
private void reassembleArray(int[] array, ArrayList<ArrayList<Integer>> buckets) {
Iterator<ArrayList<Integer>> it = buckets.iterator();
int i = 0;
while (it.hasNext()) {
ArrayList<Integer> intList = it.next();
Iterator<Integer> itInteger = intList.iterator();
while (itInteger.hasNext()) {
array[i] = itInteger.next();
i++;
}
}
}
使用这种简单的形式有任何危险或副作用吗?
这是完整的程序,它是称为基数排序的排序算法的实现。
import java.util.ArrayList;
import java.util.Iterator;
/**
* Radix sort implementation
*/
public class RadixSort {
/**
* Runs the program
*/
public static void main(String[] args) {
int[] array = {315, 418, 591, 260, 533, 58, 976, 938,};
System.out.println("Radix sort implementation");
printArray(array);
sort(array, 3); // 3 is the maximun number of digits in all array elements to sort
printArray(array);
}
/*
* sort array of integers using radix sort algorithm
* @param array The array
* @param n the maximum number of digits in array elements
*/
private void sort(int[] array, int n) {
int digitNumber = 0;
while (digitNumber < n) {
ArrayList<ArrayList<Integer>> buckets = initBuckets();
// store each element in the bucket corresponding to the (digitNumber + 1)th
// digit of that element
for (int i = 0; i < array.length; i++) {
int value = array[i];
int bucket = (value / (int) (Math.pow(10, digitNumber))) % 10;
buckets.get(bucket).add(value);
}
reassembleArray(array, buckets);
digitNumber++;
}
}
/*
* Initialize buckets ArrayList
* @return The buckets ArrayList
*/
private ArrayList<ArrayList<Integer>> initBuckets() {
ArrayList<ArrayList<Integer>> buckets = new ArrayList<ArrayList<Integer>>();
// a bucket for each digit from 0 to 9
for(int i = 0; i < 10; i++) {
buckets.add(new ArrayList<Integer>());
}
return buckets;
}
/*
* Reassemble the array
* @param array The array
* @param buckets The buckets
*/
private void reassembleArray(int[] array,
ArrayList<ArrayList<Integer>> buckets) {
Iterator<ArrayList<Integer>> it = buckets.iterator();
int i = 0;
while (it.hasNext()) {
ArrayList<Integer> intList = it.next();
Iterator<Integer> itInteger = intList.iterator();
while (itInteger.hasNext()) {
array[i] = itInteger.next();
i++;
}
}
}
/*
* Prints an array of integers on a single line
* @param array The array
*/
private void printArray(int[] array) {
System.out.print("array: {");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ((i == array.length - 1) ?"" : ", "));
}
System.out.println("}");
}
}
该算法只需要通过多个步骤对集合进行排序, 首先按照右边第一个数字对集合进行排序,并按照最后一个数字对 0 - 9 桶中的所有元素进行排序,同时保持集合的初始顺序,然后从第 0 个桶开始重新组合集合,然后按照左边的下一个数字进行排序,依此类推。
该程序确实可以正常工作,并且可以很好地对集合进行排序。
最佳答案
我敢打赌,您得到的输出与您期望的不同,因为以下行结果是:
array[i] = itInteger.next();
Type mismatch: cannot convert from Integer to int[]
据我了解,您想要转换 List<List<Integer>>
结构到相同类型的数组。您还需要映射Integer
至int
使用Stream::mapToInt
:
int array[][] = new int[buckets.size()][];
IntStream.range(0, buckets.size())
.forEach(i -> array[i] = buckets.get(i)
.stream()
.mapToInt(j -> j.intValue())
.toArray());
System.out.println(Arrays.deepToString(array)); // Prints the very same output
此外,您不需要使用Iterator
,但是如果您坚持这样做:
int outerIndex = 0;
while (outerIterator.hasNext()) {
List<Integer> list = outerIterator.next();
Iterator<Integer> innerIterator = list.iterator();
array[outerIndex] = new int[list.size()];
int innerIndex = 0;
while (innerIterator.hasNext()) {
array[outerIndex][innerIndex] = innerIterator.next().intValue();
innerIndex++;
}
outerIndex++;
}
Are there any dangers or side effects for using this simple form?
是的,这两种解决方案都是因为它们不会修改输入的原始资源,也不会影响其他资源。
关于java - 迭代多维 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910558/