我想搜索二维对象数组并根据对象类字段分别测试 1 和测试 2 分隔对象。然后,我想将 2d 数组对象的索引写入两个 1d 数组作为 x,y。我想为每个对象提供两对两个 1d 数组,以便我可以计算测试 1 和测试 2 对象之间的距离。
我的问题/疑问当我在其中一个 1d 数组上运行循环以打印它们的值以检查它们时,它们充满了一堆零,它们不应该。我在代码中包含注释以帮助澄清。
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);
}
}
我认为这里的问题是您对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++;
}}}
好吧,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);
}
}