Java 我如何画一个“a”;瓷砖“;从一个数组?
我想画一个数组,但我不知道正确的方法。 这是我的数组Java 我如何画一个“a”;瓷砖“;从一个数组?,java,arrays,tile,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} }; 我知道我错过了很多,而且我似乎
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[]map=
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,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行=5;
int cols=5;
int i,j;
对于(i=0;i
使用网格窗格:
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
}
}
}
}
publicstaticvoid打印图像(idPicture,path){
图像=新图像(路径);
this.idPicture=idPicture;//在您的示例中为0或1
对于(int i=0;i
这对我来说是最简单的方法。但您必须对要打印的每个图像重复此过程,设置ID和路径。如果您只想在控制台中打印它们,则可以尝试循环。不过,您也可以使用Java8的一些魔力
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
“绘制数组”是什么意思?你能举个例子吗?你想以某种方式“打印”它吗?可能是循环?你应该从映射数组中获取长度,而不是硬编码行和列变量。我在代码中构建了更多的apon,这就是我最后使用的
int[]map={{1,1,1,1,1},{0,0,1,0,0},{1,1,1,1,1,1},{0,1,1,1},{1,1,1};int行=5;int cols=5;int i,j;对于(i=0;i