二维数组的遍历

public class TestArray {

    private static Point[][] mPoints = new Point[3][4];

    public static void main(String[] args) {
        //第一行
        mPoints[0][0] = new Point(0, 0);
        mPoints[0][1] = new Point(0, 1);
        mPoints[0][2] = new Point(0, 2);
        mPoints[0][3] = new Point(0, 3);
        //第二行
        mPoints[1][0] = new Point(1, 0);
        mPoints[1][1] = new Point(1, 1);
        mPoints[1][2] = new Point(1, 2);
        mPoints[1][3] = new Point(1, 3);
        //第三行
        mPoints[2][0] = new Point(2, 0);
        mPoints[2][1] = new Point(2, 1);
        mPoints[2][2] = new Point(2, 2);
        mPoints[2][3] = new Point(2, 3);

        System.out.println("mPoints.length===========" + mPoints.length + "行");
        for (int i = 0; i < mPoints.length; i++) {//遍历行
            Point[] mPoint = mPoints[i];
            System.out.println("mPoint.length==========" + mPoint.length + "列");
            for (int j = 0; j < mPoint.length; j++) {//遍历列
                System.out.println(i + "行======" + j + "列===值为===" + mPoint[j].toString());
            }
        }
    }

    static class Point {

        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        @Override
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }

}

打印结果:

mPoints.length===========3行
mPoint.length==========4列
0行======0列===值为===Point{x=0, y=0}
0行======1列===值为===Point{x=0, y=1}
0行======2列===值为===Point{x=0, y=2}
0行======3列===值为===Point{x=0, y=3}
mPoint.length==========4列
1行======0列===值为===Point{x=1, y=0}
1行======1列===值为===Point{x=1, y=1}
1行======2列===值为===Point{x=1, y=2}
1行======3列===值为===Point{x=1, y=3}
mPoint.length==========4列
2行======0列===值为===Point{x=2, y=0}
2行======1列===值为===Point{x=2, y=1}
2行======2列===值为===Point{x=2, y=2}
2行======3列===值为===Point{x=2, y=3}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值