这可能看起来很愚蠢,我有一个名为船舶位置的类,我希望存储我所有的船舶位置,我从我的客户端类扩展了它,并简单地调用了 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];
}
}
}
}
我会将其作为 sub.local 传递吗? 输出是 [[I@a401c2
最佳答案
而不是写
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 .
关于java - 在一个类中初始化一个数组并使其可供另一个类访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14329625/