迭代二维Java数组 [英] Iterating over bidimensional Java arrays

查看:138
本文介绍了迭代二维Java数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static List<Vertex<Integer>> petersenGraph() {
    List<Vertex<Integer>> v = new ArrayList<Vertex<Integer>>();

    for (int i = 0; i < 10; i++) {
        v.add(new Vertex<Integer>(i));
    }

    int[][] edges =
    {{0,1}, {1,0}, {1,2}, {2,1}, {2,3}, {3,2}, {3,4}, {4,3}, {4,0}, {0,4},
    {5,6}, {6,5}, {6,7}, {7,6}, {7,8}, {8,7}, {8,9}, {9,8}, {9,5}, {5,9},
    {5,0}, {0,5}, {6,2}, {2,6}, {7,4}, {4,7}, {8,1}, {1,8}, {9,3}, {3,9}};

    for (int[] e : edges)
        v.get(e[0]).successors().add(v.get(e[1]));

    return v;
}

我理解所有事情,直到迭代边缘的地方。究竟是怎么回事?

I understand everything up to the point where there's the for which iterates over the edges. What is exactly is going on there?

编辑:为什么要使用 e [0] 和<$来访问它们C $ C> E [1] ?是 e [0] 第一个数字和 e [1] 第二个?

edit: why are they accessed using e[0] and e[1]? is e[0] the first number and e[1] the second?

推荐答案

哎呀,这太丑了。

edge是一个二维数组,所以它是一个int数组阵列。在实际定义中,它是一对数组。

edges is a bidimensional array, so it is an array of int arrays. In the actual definition, it is an array of pairs.

(int [] e:​​edges)的行简单地表示在每次迭代中,e将成为不同的int数组,因此在每次迭代中它都是不同的对。

The line for (int[] e: edges) simply means that in each iteration, e will become a different array of ints, so in each iteration it is a different pair.

然后,e [0]代表货币对中的第一项,e [1]代表另一项。
因此,第一个坐标用于查找顶点,然后发生一些事情并添加第二个坐标。如果没有看到顶点或知道算法,则不清楚。

Then, e[0] represents the first item in the pair and e[1] represents the other. So the first coordinate is used to look up a vertex, and then something happens and the second coordinate gets added in. Without seeing vertex or knowing the algorithm, it's unclear.

这篇关于迭代二维Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆