1

これはばかげているように思えるかもしれませんが、すべての船の位置を保存したい船の位置と呼ばれるクラスがあり、それをクライアントクラスから拡張し、次のように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

4

1 に答える 1

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へのリンクは次のとおりです。

出力の説明については、この回答を参照してください。

于 2013-01-15T01:15:33.063 に答える