简体   繁体   中英

How to store co-ordinates using a two dimensional array?

I am doing a project involving a two dimensional array. The project involves a dot that moves from the top left corner to the bottom right corner. What I am doing right now is making an array of possibleXMoves and an array of possibleYMoves. I was wondering if there was a better way to define coordinates for a two dimensional array. Below is what I have.

int moveX1 = x + 1;
    int moveX2 = x + 1;
    int moveX3 = x + 1;
    int moveX4 = x;
    int moveX5 = x - 1;
    int moveX6 = x - 1;
    int moveX7 = x - 1;
    int moveX8 = x;
    int [] possibleXIndexes = {moveX1, moveX2, moveX3, moveX4, moveX5, moveX6, moveX7, moveX8};

    int moveY1 = y - 1;
    int moveY2 = y;
    int moveY3 = y + 1;
    int moveY4 = y + 1;
    int moveY5 = y + 1;
    int moveY6 = y;
    int moveY7 = y - 1;
    int moveY8 = y - 1;
    int [] possibleYIndexes = {moveY1, moveY2, moveY3, moveY4, moveY5, moveY6, moveY7, moveY8};

You could use the Point class.

Instead of storing the possible indexes in separate arrays, just store them in one as a collection of Points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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