【发布时间】:2015-11-16 00:47:43
【问题描述】:
我正在尝试打印一个带有随机数的 2d 网格和另一个相同大小的网格,其中 -1 代替可被 3 整除的数字。我完成了几乎所有内容,但我不知道如何制作以下线路工作:Arraycopy = c.createCoords({10,10});
我知道我应该使用 int[][] 调用该方法,但我不记得如何了。
以下是所有代码以防万一:
public class practice
{
public int [][] createArray(int rSize, int cSize)
{
Random r = new Random();
int[][] array = new int [rSize] [cSize];
for (int row = 0; row <array.length; row++)
{
for(int col = 0; col < array[0].length; col++)
{
array[row][col] = r.nextInt(25);
}
}
return array;
}
public void print2DArray(int[][] Array)
{
for (int row = 0; row <Array.length; row++)
{
for(int col = 0; col < Array[0].length; col++)
{
System.out.print(Array[row][col] + "\t");
}
System.out.println("\n");
}
}
public int[][] createCoords(int[][] Array)
{
int [] [] coord = {
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
{-1,-1,-1, -1, -1, -1, -1, -1, -1, -1}
};
for (int row = 0; row <coord.length; row++)
{
for(int col = 0; col < coord[0].length; col++)
{
System.out.print(coord[row][col] + "\t");
}
System.out.println("\n");
}
for (int row = 0; row <coord.length; row++)
{
for(int col = 0; col < Array[row].length; col++)
{
if(Array[row][col] %3 == 0)
coord[row][col] = -1;
}
}
return coord;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
practice c = new practice();
int [][] myArray;
int [][] Arraycopy;
myArray = c.createArray(10, 10);
Arraycopy = c.createCoords({10,10});
c.print2DArray(Arraycopy);
c.print2DArray(myArray);
}
}
【问题讨论】:
-
print2DArray(new int[][]{{1,2,2},{3,3,4}});
标签: java arrays debugging multidimensional-array methods