-3

I want to search through a 2d object array and separate the objects based on an object class field, test 1 and test 2 respectively. I then want to write the 2d array's object's indices to two 1d arrays as x,y. I would like to have two pairs of two 1d arrays for each object, so that I can calculate the distance between the test 1 and test 2 object.

My problem/Question When I run a loop on one of the 1d arrays to print their values to check them, they are filled with a bunch of zeros and they shouldn't be. I included comments in the code to help clarify.

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]);

    }

    }
}

// This is the object class.

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);

}
}
15
  • Is the number of rows and columns equal in you objectArray? Jul 8, 2016 at 4:34
  • Yes its 8 by 8 Character objectArray[][] = new Character[8][8]; Jul 8, 2016 at 4:36
  • What is your question? Jul 8, 2016 at 4:38
  • I want to find the indices of the objects in my 2d object array and save them to 1 d arrays, x and y respectively, so I can calculate the distance between them. Jul 8, 2016 at 4:41
  • @BurtReynolds That's fine. What is it that you are asking us, specifically? Jul 8, 2016 at 4:42

2 Answers 2

1

I think the problem here is that you are using a same index(x, y) for test1x1, test1Y1, test2X2, test2Y2 arrays. try using different index name for those arrays. because I think they are 4 different arrays. for example :

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++;
}}}
3
  • If this were the problem he wouldn't be getting a bunch of 0s littered throughout his arrays.
    – Jesse
    Jul 8, 2016 at 5:15
  • My bad, @Mast.mangesh. you are correct. I didn't understand at first what you were saying.
    – Jesse
    Jul 9, 2016 at 5:28
  • Thanks I see what you're saying. Jul 9, 2016 at 6:16
0

Ok. Mast.mangesh was correct, he just didn't have the full context. You are indexing into your test arrays with x and y but you aren't adding values to your test arrays at every x and y. They should have their own index that is incremented only when you add something to the test array itself, which brings me to my next suggestion. Why are you using arrays here? Why not use something more robust, like ArrayList? You would have avoided that all along...

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);
    }
}
3
  • Thanks I will give this a try. I have only been programming for a few weeks. I didn't know about ArrayList. Jul 9, 2016 at 5:57
  • Thanks so much for your help this works and I learned something new. Jul 9, 2016 at 6:01
  • Glad it helped. Keep on coding! If you're only a few weeks in, check out codeacademy.com they do a pretty good job on the introductions.
    – Jesse
    Jul 9, 2016 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.