在一个类中初始化一个数组,并使另一个类可以访问

时间:2013-01-15 01:11:07

标签: java arrays

这可能看起来很愚蠢我有一个叫做船舶位置的类,我希望存储我所有的船只位置,我从我的客户端类扩展它并简单地调用set方法如下 sub.local是来自船舶类的多维数组

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

当我通过另一个类打印回来时,它会返回内存中的位置而不是我需要的数字。这是什么原因

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

}



}

将im作为sub.local传递给我吗? 输出是[[I @ a401c2

1 个答案:

答案 0 :(得分:2)

而不是写

System.out.println(yourArray);

使用

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

以下是相关JavaDoc的链接。

有关输出的说明,请查看this answer