这个问题有点合乎逻辑,我没有任何逻辑。到目前为止,我已经尝试了一切。
假设有一组数字:
[23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
因为我以平铺的方式编写游戏。我试图将它安装在2D阵列中。
宽度为12块宽,高度为8块宽。 (每块64px宽(不相关))。
所以在上面数字数组中的每12个数字之后,我希望程序转移到下一行,产生类似于的结果:
long[][] mapData = new long[(int) width][(int) height];
mapData[0][0] = 23;
mapData[0][1] = 23
mapData[0][2] = 23
mapData[0][3] = 23
mapData[0][4] = 23
...
mapData[0][8] = 23
(现在程序发现它有8个宽度,所以它移动到下一行)
mapData[1][0] = 23...
它一直持续到它完成所有这一切。
答案 0 :(得分:0)
试试这个:
long[][] mapData = new long[(int) width][(int) height];
for(int i=0; i < height; i++){
for(int u=0; u < width; u++){
mapData[i][u] = 23;
}
}
将2D阵列成像为表格。程序循环通过表的第一行,用'23'填充该行的每一列。该行完成后,外部for循环计数i ++,因此切换到表的下一行。依此类推,直到每个字段的值都为'23'。您当然可以使用您喜欢的任何值更改mapData [i] [i]。
希望这有帮助。
答案 1 :(得分:0)
请注意,您所说的内容和您提供的代码不匹配。
通常,矩阵读为matrix[row][column]
所以:
matrix[0][n]
- means first row column n
matrix[1][n]
- means second row column n
无论如何,如果你将它们混合在一起并且使用matrix[0][n]
作为first column row n
,(换句话说matrix[column][row]
,那么只需更改我的代码即可准确反映你想要的内容。
我想要的是:
Height of matrix is 12; Width is 8
而不是反之亦然。
这应该有效:
private static final int[] array =
{ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 26, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
private static final int height = 12;
private static final int width = 8;
public static void main(String[] args) {
int[][] result = new int[height][width];
int index = 0;
//write on rows first then columns
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
result[row][col] = array[index];
index++;
}
}
//print the matrix
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (col < 7) {
System.out.print(result[row][col] + "\t");
} else {
System.out.print(result[row][col] + "\n");
}
}
}
}
将打印(12行,8列):
23 23 23 23 23 23 23 23 23 23 23 23 31 31 31 31 31 31 31 31 31 31 31 31 26 4 4 4 4 4 4 4 4 4 4 4 26 4 4 4 4 4 4 4 4 4 4 4 26 4 4 4 4 4 4 4 4 4 4 4 26 4 4 4 4 4 4 4 4 4 4 4 26 4 4 4 4 4 4 4 4 4 4 4 26 4 4 4 4 4 4 4 4 4 4 4
如果您将矩阵理解为matrix[col][row]
并且实际上希望输出矩阵具有height = 8
和width = 12
,那么请在我的代码中进行以下更改:
private static final int height = 8;
private static final int width = 12;
public static void main(String[] args) {
int[][] result = new int[height][width];
int index = 0;
//write on columns first, then rows
for (int j = 0; j < width; j++) {
for (int i = 0; i < height; i++) {
result[i][j] = array[index];
index++;
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j < 11) {
System.out.print(result[i][j] + "\t");
} else {
System.out.print(result[i][j] + "\n");
}
}
}
}
将打印(8行,12列):
23 23 31 26 4 4 26 4 4 26 4 4 23 23 31 4 4 4 4 4 4 4 4 4 23 23 31 4 4 4 4 4 4 4 4 4 23 23 31 4 4 4 4 4 4 4 4 4 23 31 31 4 26 4 4 26 4 4 26 4 23 31 31 4 4 4 4 4 4 4 4 4 23 31 31 4 4 4 4 4 4 4 4 4 23 31 31 4 4 4 4 4 4 4 4 4
答案 2 :(得分:0)
这可能有所帮助:
int array[]=[23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 26, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
long[][] mapData = new long[(int) width][(int) height];
int k = 0;
for(int i=0;i<width;++i){
for(int j=0;j<height;++j){
mapData[i][j]=array[k++];
}
}