如何从数组中绘制“tile”?

时间:2016-01-03 21:18:29

标签: java arrays tile

我正在尝试绘制数组,但我不知道执行该数组的正确方法。 这是我的阵列。

int[][] map=
            {
                    {1,1,1,1,1},
                    {0,0,1,0,0},
                    {1,1,1,1,1},
                    {0,0,1,0,0},
                    {1,1,1,1,1}

            };

我知道我错过了很多,我似乎找不到任何初学者编码器可以理解的答案。

3 个答案:

答案 0 :(得分:0)

int[][] map=
            {
                    {1,1,1,1,1},
                    {0,0,1,0,0},
                    {1,1,1,1,1},
                    {0,0,1,0,0},
                    {1,1,1,1,1}

            };

int rows = 5;
int cols = 5;
int i, j;

for (i = 0; i < rows; i++) {
  for (j = 0; j < cols; j++) {
    System.out.print(map[i][j] + " ");
  }
System.out.println("");
}

答案 1 :(得分:0)

使用GridPane:

public static void printImage(idPicture, path){
        image = new Image(path);
        this.idPicture = idPicture; //in your case 0 or 1

        for(int i = 0; i < width; i++){
            for (int j = 0; j< height; j++){
                if(map[i][j] == idPicture){ //check the current id
                    imageViewMatrix[i][j] = new ImageView(image); //load the image in the matrix
                    imageViewMatrix[i][j].setPreserveRatio(true);
                    imageViewMatrix[i][j].setFitWidth(imageWidth); 
                    pane.add(imageViewMatrix[i][j], i, j); //add image to the pane
                }
            }
        }
    }

这对我来说是最简单的方法。但是,您必须为要打印的每个图像重复此过程,设置ID和路径。

答案 2 :(得分:0)

如果您只想在控制台中打印它们,那么您可以尝试循环。但是你也可以使用Java 8的一些魔力。

public static void main(String[] args) {
    System.out.println("Started");
    int[][] map=
        {
                {1,1,1,1,1},
                {0,0,1,0,0},
                {1,1,1,1,1},
                {0,0,1,0,0},
                {1,1,1,1,1}

        };

    Arrays.asList(map).stream().forEach((a) -> System.out.println(Arrays.toString(a)));

    System.out.println("");

    Arrays.asList(map).stream().forEach((a) -> {
        String b = Arrays.toString(a);
        System.out.println(b.substring(1, b.length() - 1));
    });

    System.out.println("");

    Arrays.asList(map).stream().forEach((a) -> {
        String b = Arrays.toString(a);
        System.out.println(b.substring(1, b.length() - 1).replaceAll(",", " "));
    });
}

<强>输出

Started
[1, 1, 1, 1, 1]
[0, 0, 1, 0, 0]
[1, 1, 1, 1, 1]
[0, 0, 1, 0, 0]
[1, 1, 1, 1, 1]

1, 1, 1, 1, 1
0, 0, 1, 0, 0
1, 1, 1, 1, 1
0, 0, 1, 0, 0
1, 1, 1, 1, 1

1  1  1  1  1
0  0  1  0  0
1  1  1  1  1
0  0  1  0  0
1  1  1  1  1
相关问题