简体   繁体   English

Java 2D数组-如何将数组分配为值?

[英]Java 2D array - How to assign an array as a value?

Background 背景
I am doing a game project for my java class and we are using a grid system for the playing area. 我正在为Java类做一个游戏项目,并且在游戏区域使用了网格系统。 50 wide by 25 tall. 50宽乘25高。 The squares are dynamically sized based on your monitor size and it involves a bit of math. 正方形是根据您的显示器大小动态调整大小的,其中涉及一些数学运算。 I removed that from my example below so it is easier to look at. 我从下面的示例中删除了它,因此更易于查看。

What I currently decided to do is store the grid squares in a nice and simple 2D array so we can access and update the playing area as needed. 我目前决定要做的是将网格正方形存储在一个漂亮且简单的2D数组中,以便我们可以根据需要访问和更新游戏区域。 Here is that code: 这是该代码:

// Updates when told to, saves new values
public static void GridList() {
// Record which grid squares are open (0) or blocked (1)
// X = Row and Y = Column
for (int x = 0; x < 50; x++ ) {
    for ( int y = 0; y < 25; y++ ) {
        gridList[x][y] = 0;
    }
}
}

Our Problem 我们的问题
Now the trouble starts when I move on to saving order pairs (x,y) that represent the middle of these grid squares. 现在,当我继续保存代表这些网格正方形中间的订单对(x,y)时,麻烦就开始了。 For example, based on all the math we did to figure out your monitor size, we made a grid 50 wide by 25 tall and now need to save the (x,y) coordinates of the middle of those squares. 例如,基于所有用于计算显示器尺寸的数学运算,我们制作了一个宽50 x高25的网格,现在需要保存这些正方形中间的(x,y)坐标。 This is so our AI knows where to move enemies; 这样我们的AI知道了将敌人移至何处。 point by point as long as it is open. 只要打开一点一点。

This is what I have so far that saves X coordinates and Y coordinates in their own arrays: 到目前为止,这是我将X坐标和Y坐标保存在它们自己的数组中的功能:

public static void NodeList() {
    for (int x = 0; x < 50; x++ ) {
        for ( int y = 0; y < 25; y++ ) {
            nodeListX[x][y] = *Removed all the math.*;
            nodeListY[x][y] = *Removed all the math.*;
        }
    }
}

What We Are Aiming To Have 我们想要拥有的
What I would really like to do is save an array for every grid square like this: 我真正想做的是为每个网格正方形保存一个数组,如下所示:

public static void NodeList() {
    for (int x = 0; x < 50; x++ ) {
        for ( int y = 0; y < 25; y++ ) {
            nodeList[x][y] = *array{x,y}*;
        }
    }
}

Is this possible in Java? 这在Java中可行吗? I can't figure this out. 我不知道这个。 I saw things mentioned about lists but we have not covered that yet so I am at a loss. 我看到了有关列表的内容,但我们还没有涵盖,所以我很茫然。

Java doesn't really have a way to store a pair of numbers. Java确实没有存储一对数字的方法。 But you could make a coordinate class like this: 但是您可以创建一个坐标类,如下所示:

`public class Coordinate
 int x;
 int y;

 public Coordinate(x,y)
 {
  this.x=x;
  this.y=y;
 }

 public int getX()
 {
  return x;
 }

 public int gety()
 {
  return y;
 }
`

Then you can just create an array of coordinates. 然后,您可以只创建一个坐标数组。

Honestly, I don't see the problem with your or fdsa's solution. 老实说,我看不到您或fdsa解决方案的问题。 But if you're looking for another approach, you can always use a 3-dimensional array, with the third dimension containing 2 elements for X and Y: 但是,如果您正在寻找另一种方法,则始终可以使用3维数组,第3维数组包含X和Y的2个元素:

public static void NodeList() {
    for (int x = 0; x < 50; x++ ) {
        for ( int y = 0; y < 25; y++ ) {
            int xPos = *Removed all the math.*;
            int yPos = *Removed all the math.*;
            nodeList[x][y] = new int[] {xPos, yPos};
        }
    }
}

Just make sure to declare nodeList as int[][][] . 只需确保将nodeList声明为int[][][]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM