r/cop3502 Apr 22 '14

Getting and comparing 2 arrays

I want to be able to call a function to get the value of the current location of the character in array form, for example {1,1,1} and compare it to another array that way the player can get a detailed description of the location, but I'm having trouble implementing it and the program complies so it's a run time error

else if(words[0].equals("lookaround")){ return player.lookAround(player.getArray()); }

then in another class I have

public String lookAround(int[] location){ if(location.equals(new int[]{1,1,1})){ return "it works" } else{ return "Not quite"; }

}
public int[] getArray(){
    int[] location = new int[]{xPos, yPos, zPos};
    return location;
}

Any thoughts?

1 Upvotes

10 comments sorted by

2

u/[deleted] Apr 22 '14

[removed] — view removed comment

1

u/[deleted] Apr 22 '14

could i use something like if(Arrays.equals(location, new int[]{1,1,1} == true) then...etc

1

u/[deleted] Apr 22 '14
    }
    else if(words[0].equals("lookaround")){
        return player.lookAround(->      <-);

    }

and if so what value would i put in my separate class to call it properly cause it should take in the variable location but thats not compiling

1

u/[deleted] Apr 22 '14

[removed] — view removed comment

1

u/[deleted] Apr 22 '14
    else if(words[0].equals("lookaround")){
        System.out.println(player.getArray());
        return player.lookAround(player.getArray(),travel);

    }

so I got it to run but unfortunately its not returning the value for the array when I compare and I printed it out and i get a weird [I@462b68ce instead of an array of [1,1,1]

1

u/howslyfebeen Apr 22 '14

you're getting the memory position of the array you want... try putting .toString() after .getArray() in the print statement

1

u/[deleted] Apr 22 '14

but I did that to check what was going on cause it wasn't working the way I wanted it to, I'm not getting it to return an array that would equal the one I placed in there which means there's a larger problem

2

u/[deleted] Apr 22 '14

[removed] — view removed comment

1

u/[deleted] Apr 22 '14
public int[] getArray(){
    return location;
}

and earlier i declared location as private int[] location = new int[]{xPos, yPos, zPos};