简体   繁体   中英

I want to store a 2d Char array into an ArrayList

First of all, I have checked previous posts and still could not find an answer. So here is my question: I want to store a 2d char array showing a specific state of a chess board (for example the starting state) into an ArrayList . An example of a state would be:

国际象棋州

How would I store this state (being a 2d char array called chessBoard) into an ArrayList called chessState?

The names do not matter but I thought it would help to provide any help you can.

My previous attempts have gotten me "C@22e7c30e" in my output.

Thank you for any help offered!

Why don't you make a class ChessState containing the 2D Char Array and then make a an arraylist of those states?

public class ChessState {
...
}



public class Chess {
...

ArrayList<ChessState> = ...

...
}
ArrayList<char[][]> ChessState= new ArrayList<char[][]>();
ChessState.add(chessBoard);

Generally you can put whatever you like in an array . If you want more information about the chessboard do it with an object. The code above should work for a 2d char array.

public char[][] possibleChessMove = new char[8][8];

ArrayList<char[][]> storeMoves = new ArrayList<>();

and after filling the possibleChessMove to the starting board state I did this:

 storeMoves.add(possibleChessMove);

                        for (int i=0;i<storeMoves.size();i++ ) {
                            for (int j=0;j<8;j++) {
                                for (int k=0;k<8;k++) {
                                    System.out.print(storeMoves.get(i)[j][k]+" ");

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

Originally the question I asked was how do I store them, because I did think I was storing them wrong. But in the end the way I was printing it was incorrect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM