j bel
j bel

Reputation: 153

initialise a array in one class and make accessible to another

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

Upvotes: 1

Views: 167

Answers (1)

jlordo
jlordo

Reputation: 37813

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.

Upvotes: 2

Related Questions