Java 将二维对象数组标记保存到一维数组
我想搜索一个二维对象数组,并分别基于对象类字段test1和test2分离对象。然后我想将2d数组的对象索引写入两个一维数组,如x,y。我希望每个对象有两对两个一维数组,这样我可以计算测试1和测试2对象之间的距离 我的问题 当我在一个一维数组上运行一个循环来打印它们的值来检查它们时,它们被一堆零填充,不应该被填充。我在代码中加入了注释以帮助澄清Java 将二维对象数组标记保存到一维数组,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我想搜索一个二维对象数组,并分别基于对象类字段test1和test2分离对象。然后我想将2d数组的对象索引写入两个一维数组,如x,y。我希望每个对象有两对两个一维数组,这样我可以计算测试1和测试2对象之间的距离 我的问题 当我在一个一维数组上运行一个循环来打印它们的值来检查它们时,它们被一堆零填充,不应该被填充。我在代码中加入了注释以帮助澄清 public class gameboard2 { public static void main(String[] args) { Charac
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]);
}
}
}
我认为这里的问题是,您对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++;
}}}
inti=0;j=0;k=0;l=0;
for(int x=0;x
好的。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);
}
}
公共静态无效测试(){
Character2 objectArray[][]=新字符2[8][8];
ArrayList x1=新的ArrayList();
ArrayList y1=新的ArrayList();
ArrayList x2=新的ArrayList();
ArrayList y2=新的ArrayList();
ArrayList junkx=新的ArrayList();
ArrayList junky=新的ArrayList();
for(int row=0;row if(row)是objectArray中相等的行数和列数?是它的8×8字符objectArray[][]=新字符[8][8];你的问题是什么?我想在我的2d对象数组中找到对象的索引,并将它们分别保存到一维数组x和y中,这样我就可以计算它们之间的距离。@BurtReynolds很好。你具体问我们的是什么?如果这是个问题,他就不会在整个数组中乱丢一堆0了s、 我的错,@Mast.mangesh。你是对的。一开始我不明白你在说什么。谢谢,我明白你在说什么。谢谢,我会尝试一下。我只做了几个星期的编程。我不知道ArrayList。非常感谢你的帮助,这很有效,我学到了一些新东西。很高兴它有帮助。继续编码!如果你只是个f几周后,去codeacademy.com看看,他们在介绍方面做得很好。
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);
}
}