0

我想做这样的事情:

int[][] A = ... // initialize matrix
List<Integer> rows = new LinkedList<Integer>();
for (int r = 0; r < 1000000; r++) rows.add(r);
Iterator<Integer> iter = rows.iterator();
for ( ; iter.hasNext(); ) {
    // some condition based on which I remove an element
    if (200 != A[iter.next().intValue()][0]) iter.remove();
}
if (rows.isEmpty()) {
    System.out.println("empty");
}

但它不起作用。

4

2 回答 2

2

你很有可能得到一个ArrayIndexOutOfBoundsException. 这是因为该值已超出您在部分A[iter.next().intValue()]中设置的行范围。initialize matrix

假设您确实有行,您可以使用:

for (int r = 0; r < A[0].length; r++) {
...
于 2012-09-22T12:29:04.917 回答
0

好吧,它确实对我有用:

public class Test { 

    public static void main(String args[]) {
        int[][] A = new int[1000000][1];
        List<Integer> rows = new LinkedList<Integer>();
        for (int r = 0; r < 1000000; r++) rows.add(r);
        Iterator<Integer> iter = rows.iterator();
        for ( ; iter.hasNext(); ) {
            // some condition based on which I remove an element
            if (200 != A[iter.next().intValue()][0]) iter.remove();
        }
        if (rows.isEmpty()) {
            System.out.println("empty");
        }
    }
}

和打印empty

于 2012-09-22T12:22:21.423 回答