【问题标题】:Saving 2d object array indicies to 1d arrays将 2d 对象数组索引保存到 1d 数组
【发布时间】:2016-07-08 04:28:15
【问题描述】:

我想搜索二维对象数组并根据对象类字段分别测试 1 和测试 2 分离对象。然后我想将 2d 数组的对象索引写入两个 1d 数组作为 x,y。我想为每个对象有两对两个一维数组,以便我可以计算测试 1 和测试 2 对象之间的距离。

我的问题/问题 当我在其中一个 1d 数组上运行循环以打印它们的值以检查它们时,它们填充了一堆零,它们不应该是。我在代码中包含了 cmets 以帮助澄清。

public class gameboard2 {



public static void main(String[] args) {

Character2 objectArray[][] = new Character2[8][8];

int test1X1[] = new int[100];
int test1Y1[] = new int[100];
int test2X2[] = new int[100];
int test2Y2[] = new int[100];
int junkX1Array[] = new int[100];
int junkY1Array[] = new int[100];   



for (int row = 0; row < objectArray.length; row++){
for(int col = 0; col < objectArray.length; col++){
    if (row <= 1 && col <= 7){
    objectArray[row][col] = new Character2();
            objectArray[row][col].setType("Test1");
            objectArray[row][col].setIdTest1(row,col);
            objectArray[row][col].objectFlag = true;

     }
    else if ((row == 6 || row == 7) && (col <= 7)){
    objectArray[row][col]= new Character2();
            objectArray[row][col].setType("Test2");
    objectArray[row][col].setIdTest2(row,col);
            objectArray[row][col].objectFlag = true;


    }
    else {
    objectArray[row][col]= new Character2();
       objectArray[row][col].setType("Test3");

    }
    }
}

for (int x = 0; x < objectArray.length; x++){
        for (int y = 0; y < objectArray.length; y++ ){


    if (objectArray[x][y].getType().compareTo("Test1") == 0){
            test1X1[x] = x;
            test1Y1[y] = y;


    }

 if (objectArray[x][y].getType().compareTo("Test2") == 0){
        test2X2[x] = x;
        test2Y2[y] = y;

       System.out.println(test2X2[x]);
      //Arrays are filled with 2d array object indices and printed as they are filled. These values appear correct. However when you print from the array (see below) its filled with a bunch of zeros.
    }

    else
        junkX1Array[x] = x;
        junkY1Array[y] = y;
    }
    }
    System.out.print("Now the newly created array will be printed");
    // Array is printed. Values differ.
            for (int b = 0; b < test2X2.length; b++)
    {
        System.out.println(test2X2[b]);

    }

    }
}

// 这是对象类。

public class Character2 {



private String id;
private String type;
boolean objectFlag = false;



public void setType(String AssignType) {
type = AssignType;
}
public String getType(){
return type;
}
public String getId(){
return id;
}


public void setIdTest1(int row, int col){
id = ("Test1" + " row: " + row + " col: " +  col);  

}
public void setIdTest2(int row, int col){
id = ("Test2" + " row: " + row + " col: " +  col);

}
}

【问题讨论】:

  • 你的objectArray中的行数和列数是否相等?
  • 是的,它是 8 x 8 字符 objectArray[][] = new Character[8][8];
  • 你有什么问题?
  • 我想在我的二维对象数组中找到对象的索引并将它们分别保存到一维数组x和y中,这样我就可以计算它们之间的距离了。
  • @BurtReynolds 没关系。您具体要问我们什么?

标签: java arrays multidimensional-array


【解决方案1】:

我认为这里的问题是您对 test1x1、test1Y1、test2X2、test2Y2 数组使用相同的索引(x,y)。尝试为这些数组使用不同的索引名称。因为我认为它们是 4 个不同的数组。 例如:

int i=0;j=0;k=0;l=0;
for (int x = 0; x < objectArray.length; x++){
for (int y = 0; y < objectArray.length; y++ ){

if (objectArray[x][y].getType().compareTo("test1") == 0){
        test2X2[i] = x;
        test2Y2[j] = y;
       i++;j++;
}

if (objectArray[x][y].getType().compareTo("test2") == 0){
        test1X1[k] = x;
        test1Y1[l] = y;
      k++;l++;
}}}

【讨论】:

  • 如果这是问题所在,他的阵列中就不会出现一堆 0。
  • 我的错,@Mast.mangesh。你是对的。一开始我没听懂你在说什么。
  • 谢谢,我明白你在说什么。
【解决方案2】:

好的。 Mast.mangesh 是正确的,他只是没有完整的上下文。您正在使用 x 和 y 对测试数组进行索引,但您不会在每个 x 和 y 处向测试数组添加值。他们应该有自己的索引,只有当你向测试数组本身添加一些东西时才会增加,这让我想到了我的下一个建议。你为什么在这里使用数组?为什么不使用更强大的东西,比如 ArrayList?你会一直避免这种情况......

public static void test(){
    Character2 objectArray[][] = new Character2[8][8];
    ArrayList<Integer> x1 = new ArrayList<>();
    ArrayList<Integer> y1 = new ArrayList<>();
    ArrayList<Integer> x2 = new ArrayList<>();
    ArrayList<Integer> y2 = new ArrayList<>();
    ArrayList<Integer> junkx = new ArrayList<>();
    ArrayList<Integer> junky = new ArrayList<>();

    for (int row = 0; row < objectArray.length; row++){
        for(int col = 0; col < objectArray.length; col++){
            if (row <= 1 && col <= 7){
                objectArray[row][col] = new Character2();
                objectArray[row][col].setType("Test1");
                objectArray[row][col].setIdTest1(row,col);
                objectArray[row][col].objectFlag = true;

             }
             else if ((row == 6 || row == 7) && (col <= 7)){
                objectArray[row][col]= new Character2();
                objectArray[row][col].setType("Test2");
                objectArray[row][col].setIdTest2(row,col);
                objectArray[row][col].objectFlag = true;
             }
             else {
                objectArray[row][col]= new Character2();
                objectArray[row][col].setType("Test3");

             }
        }
    }
    for(Character2[] c2: objectArray){
        for(Character2 c: c2){
            System.out.print(" " + c.getType() );
        }
        System.out.println();
    }

    for (int x = 0; x < objectArray.length; x++){
        for (int y = 0; y < objectArray.length; y++ ){
            if (objectArray[x][y].getType().compareTo("Test1") == 0){
                x1.add(x);
                y1.add(y);
            }

            if (objectArray[x][y].getType().compareTo("Test2") == 0){
                x2.add(x);
                y2.add(y);
            }
            else{ 
                junkx.add(x);
                junky.add(y);
            } 
        }
    }

    System.out.print("Now the newly created array will be printed");
    for(int i : y2){
        System.out.println(i);
    }
}

【讨论】:

  • 谢谢,我会试一试。我只编程了几个星期。我不知道 ArrayList。
  • 非常感谢您的帮助,我学到了一些新东西。
  • 很高兴它有帮助。继续编码!如果您只有几周的时间,请查看 codeacademy.com,他们在介绍方面做得很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 2017-12-01
  • 1970-01-01
  • 2021-04-17
  • 2020-10-03
  • 2018-09-16
相关资源
最近更新 更多