用Java替换2D数组?(Replacing in a 2D array in Java?)

编程入门 行业动态 更新时间:2024-10-10 23:25:48
用Java替换2D数组?(Replacing in a 2D array in Java?)

在这个程序中,我试图创建一个坐标平面并替换特定点的值。 如果平面中的最大值为2,则此代码创建一个最大值和最小值为2的二维数组,并对轴进行编号,如下所示:

String [][] grid = new String [max+max+2][max+max+2]; for (int i=0; i<max+max+1; i++) for (int j=0; j<max+max+2; j++) grid [i][j]="o"; for (int i=0; i<max+max+1; i++) {Integer Col=new Integer(max-i); grid [i][0]=Col.toString();} int [] firstRow= new int [max+max+1]; for (int i=0; i<firstRow.length; i++) firstRow [i]=max-max-max+i; ex: -2 -1 0 1 2 2 o o o o o 1 o o o o o 0 o o o o o -1 o o o o o -2 o o o o o

在下面的代码中,我接收机场的随机x,y坐标(例如,假设x = 1且y = -1)。 我必须在此坐标(1,-1)处用“P”替换双数组中的“o”。 当我尝试这样做时,它将“P”置于正确的x坐标(1)但不是正确的y坐标。 第二个for循环一定有问题,但我不知道它是什么。 正确的替换应如下所示:

ex: -2 -1 0 1 2 2 o o o o o 1 o o o o o 0 o o o o o -1 o o o P o -2 o o o o o Integer airx = new Integer (airport1.getX()); Integer airy= new Integer (airport1.getY()); for (int i=0; i<firstRow.length; i++) if (firstRow [i]==airport1.getX()) airx=i; String yCord= airy.toString(); for (int k=0; k<grid[0].length-1; k++) {String w =grid [k][0]; if (w==yCord) for (int i=0; i<grid[0].length-1; i++) if (grid [i][0]==airy.toString()) airy=i; grid [airy][airx+1]="A";

In this program I am trying to create a coordinate plane and replace a value at a specific point. If the maximum value in the plane is 2, this code creates a 2D array with a max and minimum of 2 and numbers the axis like so:

String [][] grid = new String [max+max+2][max+max+2]; for (int i=0; i<max+max+1; i++) for (int j=0; j<max+max+2; j++) grid [i][j]="o"; for (int i=0; i<max+max+1; i++) {Integer Col=new Integer(max-i); grid [i][0]=Col.toString();} int [] firstRow= new int [max+max+1]; for (int i=0; i<firstRow.length; i++) firstRow [i]=max-max-max+i; ex: -2 -1 0 1 2 2 o o o o o 1 o o o o o 0 o o o o o -1 o o o o o -2 o o o o o

In the following code an I receive random x,y coordinates for an airport (for this example say x=1 and y=-1). I have to replace the "o" in the double array with a "P" at this coordinate (1,-1). When I try to do this it is placing "P" at the correct x coordinate (1) but not at the correct y coordinate. There must be a problem with the second for loop, but I do not know what it is. The correct replacement should look like this:

ex: -2 -1 0 1 2 2 o o o o o 1 o o o o o 0 o o o o o -1 o o o P o -2 o o o o o Integer airx = new Integer (airport1.getX()); Integer airy= new Integer (airport1.getY()); for (int i=0; i<firstRow.length; i++) if (firstRow [i]==airport1.getX()) airx=i; String yCord= airy.toString(); for (int k=0; k<grid[0].length-1; k++) {String w =grid [k][0]; if (w==yCord) for (int i=0; i<grid[0].length-1; i++) if (grid [i][0]==airy.toString()) airy=i; grid [airy][airx+1]="A";

最满意答案

如果你改变,你的数组将更容易阅读和索引

-2 -1 0 1 2 2 o o o o o 1 o o o o o 0 o o o o o -1 o o o o o -2 o o o o o

为此,通过改变您首先生成和索引数组的方式

-2 -1 0 1 2 -2 o o o o o -1 o o o o o 0 o o o o o 1 o o o o o 2 o o o o o

创建一个变量来保存数组的大小,即

int arrSize = 2*max + 2;

创建另一个变量来保存位置(0,0)的数组索引。

int arrMid = arrSize/2;

您可以使用grid [arrMid] [arrMid]来访问位置(0,0)

在您的示例中, 例如x = 1和y = -1 ,只需执行此操作即可将该位置更新为“P”:

Integer airx = new Integer (airport1.getX()); // 1 Integer airy= new Integer (airport1.getY()); // -1 grid[arrMid + airx][arrMid + airy] = "P"; // grid[4][2] = "P";

Your array will be easier to read & index if you change from

-2 -1 0 1 2 2 o o o o o 1 o o o o o 0 o o o o o -1 o o o o o -2 o o o o o

to this, by changing how you generate & index the array in the first place

-2 -1 0 1 2 -2 o o o o o -1 o o o o o 0 o o o o o 1 o o o o o 2 o o o o o

Create a variable to hold the size of the array, i.e.

int arrSize = 2*max + 2;

Create another variable to hold the array index of position (0, 0).

int arrMid = arrSize/2;

You can use grid[arrMid][arrMid] to access position (0, 0).

And in your example, say x = 1 and y = -1, just do this to update that position to "P":

Integer airx = new Integer (airport1.getX()); // 1 Integer airy= new Integer (airport1.getY()); // -1 grid[arrMid + airx][arrMid + airy] = "P"; // grid[4][2] = "P";

更多推荐

本文发布于:2023-07-06 07:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047455.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Java   array   Replacing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!