1

this may seem daft i have a class called ship locations which i wish to store all my ships locations, ive extended it from my client class and simply called the set method as follows sub.local being a multidimensional array from the ship class

sub.local = new int[2][2];  
sub.local[0][0] =row;
sub.local[0][1]=col;
sub.local[1][0]=row;
sub.local[1][1] =col+1;

toServer.writeInt(row);
toServer.writeInt(col);
toServer.writeChar('s');


sub.placed=true;
setp1sub(sub.local);

When i print it back through another class it comes back with the location in the memory rather than the numbers i need. What is the reason for this

public class ShipLocations {


static int [][] p1sub;

public ShipLocations()
{
    p1sub = new int[2][2];
}
public int[][] getp1sub()
{
    return p1sub;
}
public void setp1sub(int[][] local) {
    for (int i = 0;i <local.length;i++)
    {
        for(int j = 0;j<local.length;j++)
        {
            p1sub [i][j]= local[i][j];
        }
    }

}



}

would it be that im passing it as sub.local ? output is [[I@a401c2

j bel
  • 153
  • 6
  • 18

1 Answers1

2

Instead of writing

System.out.println(yourArray);

use

// for multidemensional arrays:
System.out.println(Arrays.deepToString(yourArray));
// or for one dimemsional arrays:
System.out.println(Arrays.toString(yourArray));

Here is a link to the relevant JavaDoc.

For an explanation of your output, you can look at this answer.

Community
  • 1
  • 1
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • thank you but would that mean i can only retrieve my array as a string not as a int [][] also, printing the array the out put is [[0, 0], [0, 0]] all the time which isnt right – j bel Jan 15 '13 at 08:27
  • what do you mean by retrieve? – jlordo Jan 15 '13 at 08:28
  • i intend to read this and compare this to another array, but the values (if both have identical values then the ship has been hit) however currently my main concern is that it stays at [[0,0][0,0]] – j bel Jan 15 '13 at 08:47
  • 1
    @jbel you can use it like you want. `if(array[0][0] == otherArray[0][0])` or any other way you want. Your problem was with the output beeing `[[I@a401c2`. That's output for `System.out.println(array);`. I showed you a way to get nice output. If you have a different problem, feel free to write a new question. – jlordo Jan 15 '13 at 08:49