简体   繁体   English

从二维数组(Java)顺序接收元素

[英]Sequential receiving of elements from the two-dimensional array (Java)

I'm a beginner in Java, and I need to write a next() method, which will return current value from 2D array. 我是Java的初学者,我需要编写next()方法,该方法将从2D数组返回当前值。

For example, we may have: 例如,我们可能有:

int[][] values = {{1, 2}, {3, 4}, {5, 6}}; 

When we are using next() at the first time, it returns 1 , second use - 2 , third - 3 , etc. 当我们第一次使用next()时,它返回1 ,第二次使用2 ,第三次使用3 ,依此类推。

My solution is to make 1D array from that 2D array: 我的解决方案是从该2D数组制作1D数组:

 public int[] convert(int[][] array) {

        // At first, count number of all cells to set length for new 1D array.

        int count = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                count++;
            }
        }

        // Now we have length. Creating new array and filling it with data from all arrays.

        int[] result = new int[count];
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                result[index] = array[i][j];
                index++;
            }
        }
        return result;
    }

And then just get a value from that new 1D array ( position is a class field = 0 , values is also a class field - int[][] values ): 然后从新的1D数组中获取一个值( position是一个class字段= 0values也是一个class字段int[][] values ):

public int next() {
        int[] tmp = convert(values);
        int result = tmp[position];
        position++;
        return result;
    }

But it is obvious that this solution is not the best. 但是很明显,这种解决方案不是最好的。 Is there a way to do this without conversation of the arrays? 有没有办法在不进行数组对话的情况下做到这一点?

Something similar to the methods of Iterator's next() and hasNext() ? 类似于Iterator的next()hasNext()吗?

Update. 更新。 I wrote some bad code to illustrate what I want to do: 我写了一些不好的代码来说明我想做什么:

public class ArrayConverter {
    private final int[][] values;
    private int upper = 0;
    private int lower = -1;

    public ArrayConverter(int[][] values) {
        this.values = values;
    }

    public int next() {
        lower++;
        int result = 0;
        try {
            result = values[upper][lower];
        } catch (ArrayIndexOutOfBoundsException a) {
            lower = 0;
            upper++;
            try {
                result = values[upper][lower];
            } catch (ArrayIndexOutOfBoundsException r) {
                upper = 0;
                lower = -1;
                System.out.print("Reached the end of data. Indexes will be zeroed.");
            }
        }
        return result;
    }
}

That code is bad because of try/catch blocks, and I prefer to avoid it. 由于try / catch块,该代码很糟糕,我更喜欢避免使用它。 How? 怎么样?

If you use a List then you will avoid to count the number of your elements : 如果使用List ,则将避免计算元素的数量:

int[][] array = {{1, 2}, {3, 4}, {5, 6}}; 
List<Integer> list = new ArrayList<>();
for (int[] array1 : array) {
    for (int j = 0; j < array1.length; j++) {
        list.add(array1[j]);
    }
}

Then you can get your values with list.get(position); 然后,您可以使用list.get(position);获取值list.get(position);

As Mike Tian mentioned. 就像Mike Tian提到的。 it can be done by Stream API shortly: 可以通过Stream API很快完成:

OfInt iter = Stream.of(a).flatMapToInt(IntStream::of).iterator();

while (iter.hasNext()) {
    iter.nextInt();
}

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

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