繁体   English   中英

在Java中创建2D坐标数组

[英]Creating a 2D array of coordinates in java

我正在尝试创建一个二维坐标数组,每个位置的随机值在1到4之间。 我目前在初始化值时遇到麻烦。 这是该方法的当前代码:

public void createMap(){
    for (int i = 1; i < 20; i ++){
        for (int j = 1 ; j < 20; j ++) {

          coord[i][j] = setCoordinates(random.nextInt(4) + 1, random.nextInt(4) + 1);
        }
    }

    System.out.println(getCoord());
}

和这个方法:

public Coordinates setCoordinates (int row, int column){
    this.row = row;
    this.column = column;
    return coord[row][column];
}

和坐标类:

public class Coordinates {
int row;
int column;


public void setColumn(int column){
    this.column = column;
}

public void setRow(int row){
    this.row = row;
}

public int getRow(){
    return row;
}

public int getColumn(){
    return column;
}

}

结果在控制台中始终为null 如何更改代码以实际初始化数组中的值? 最终目标是为2D游戏创建坐标网格。 如果要在GUI GridPane上使用此参数(例如返回类型),我有什么特别的注意事项? 请让我知道是否需要更多信息。 在此先感谢您的帮助。

public Coordinates setCoordinates (int row, int column){
   Coordinates c = new Coordinates();
   c.setRow(row);
   c.setColumn(column);
   return c;
}

在这种情况下,坐标应为Coordinates[][] coord = new Coordinates[x][y];

我不能从您的解释中得到很多,但是我向您展示了两个完整的示例,其中包含Coordinates类,而没有:

示例1,使用setCoordinate函数返回Coordinates对象:

package com.company;

import java.io.Console;
import java.util.concurrent.ThreadLocalRandom;

public class Main {

    public static void main(String[] args) {
    // write your code here

        Coordinates[] coord = new Coordinates[20];

        createMap();
    }

    public static void createMap(){
        Coordinates[] coord = new Coordinates[20];

        for(int i = 0; i < 20; i ++){
            coord[i] = setCoordinates(ThreadLocalRandom.current().nextInt(0, 99) + 1, ThreadLocalRandom.current().nextInt(0, 99) + 1);
        }

        for(int i = 0; i < 20; i ++){
            coord[i].getCoord();
        }
    }

    public static Coordinates setCoordinates (int row, int column){
        Coordinates c = new Coordinates();
        c.setRow(row);
        c.setColumn(column);
        return c;
    }

    public static class Coordinates {
        int row;
        int column;

        public Coordinates(){
            //constructor
        }

        public void setColumn(int column){
            this.column = column;
        }

        public void setRow(int row){
            this.row = row;
        }

        public int getRow(){
            return row;
        }

        public int getColumn(){
            return column;
        }

        public void getCoord(){
            //just return and print the coordinates
            System.out.println("ROW: " + this.getRow() + "      COL: " + this.getColumn());
            //change void to return and return value if you like :)
        }

    }
}

如果您愿意,可以使用getCoord()并通过知道[0]是行而[1]是列来返回坐标数组,如下所示:

public int[] getCoord()
        {
            int coords[] = new int[2];

            coords[0] = this.getRow();
            coords[1] = this.getColumn();

            return coords;
        }

然后可以像这样打印

for(int i = 0; i < 20; i ++){
            int coords[] = coord[i].getCoord();
            System.out.println(coords[0] + " - " + coords[1]);
        }

和示例2,不返回坐标

package com.company;

import java.io.Console;
import java.util.concurrent.ThreadLocalRandom;

public class Main {

    public static void main(String[] args) {
    // write your code here

        Coordinates[] coord = new Coordinates[20];

        createMap();
    }

    public static void createMap(){
        Coordinates[] coord = new Coordinates[20];

        for(int i = 0; i < 20; i ++){
            coord[i].setCoordinates(ThreadLocalRandom.current().nextInt(0, 99) + 1, ThreadLocalRandom.current().nextInt(0, 99) + 1);
        }

        for(int i = 0; i < 20; i ++){
            coord[i].getCoord();
        }
    }

    public static class Coordinates {
        int row;
        int column;

        public Coordinates(){
            //constructor
        }

        public void setColumn(int column){
            this.column = column;
        }

        public void setRow(int row){
            this.row = row;
        }

        public int getRow(){
            return row;
        }

        public int getColumn(){
            return column;
        }

        public void setCoordinates (int row, int column){
            this.setRow(row);
            this.setColumn(column);
        }

        public void getCoord(){
            //just return and print the coordinates
            System.out.println("ROW: " + this.getRow() + "      COL: " + this.getColumn());
            //change void to return and return value if you like :)
        }

    }
}

暂无
暂无

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

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