移动数组中的单个对象

时间:2015-08-05 20:01:51

标签: java arrays

我一直在寻找答案,但没有成功。我有两个数组,我插入对象并在屏幕上打印结果。现在我要做的是将数组中的每个存储对象移动到不同的坐标并打印结果。任何帮助将不胜感激。

public class Board {

    int row = 0;
    int col = 0;
    public Ships ships=new Ships();
    Controller controller = new Controller();

    public Board(int rows,int columns)
    {
         board = new Ships[rows][columns];
         this.r=rows;
         this.c=columns;
    }

    public void addShip(int x,int y,Ships s)
    {
        board[x][y]=s;
    }

    public void print(ArrayList<Ships> player1)
    {
        for(int i = 0; i<row; i++)
        {            
           for(int j = 0; j<col; j++)
           {
               ships=board[i][j];

                if(ships==null)
                {
                    System.out.print("-");
                    System.out.print("\t");
                }
                else
                {
                    System.out.print(ships.getID());
                    System.out.print("\t");
                }
            }
            System.out.println();
            System.out.print("\n");
        }

        Scanner readinput = new Scanner(System.in);
        String enterkey = "press enter key to continue....";
        System.out.print(enterkey);

        enterkey = readinput.nextLine();
        System.out.print(enterkey);

        if (enterkey.equals("")){

            System.out.println("\n");
            System.out.println("\n");
            System.out.println("\n");
            System.out.println("\n");
            System.out.println("\n");
            System.out.println("\n");
            System.out.println("\n");
            System.out.println("\n");

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题相对不清楚,但我假设您想要将您的二维阵列(电路板)中的特定位置的船舶移动到另一个位置。

这样的方法应该做你想做的事:

public void moveShip(int xFrom, int yFrom, int xTo, int yTo){
    board[xTo][yTo] = board[xFrom][yFrom];
    board[xFrom][yFrom] = null;
}