将 char 2d 数组转换为 2d char 数组列表时遇到问题

Having problems converting a char 2d array to a 2d char array list

好吧,我一直在编写代码,将双字符数组转换为双数组列表,到目前为止,我一直在使用两个 for 循环来实现这一点,添加每个数组的字符在双数组中添加到数组列表,然后将其添加回双数组列表。下面是我的转换器的代码:

public static ArrayList<ArrayList<Character>> CharAToAL(char[][] chars){
        ArrayList<ArrayList<Character>> c = new ArrayList<>();
        ArrayList<Character> b = new ArrayList<>();

        for(int i=0; i < chars.length; i++){
            b.clear();
            for (int k=0; k < chars[i].length; k++){
                b.add(Character.valueOf(chars[i][k]));
//this part of code prints out the correct letters
                Debug.printLn(String.valueOf(chars[i][k]));
            }
            c.add(b);
        }
        return c;
    } 

我正在使用以下代码对其进行测试:

//static obj outside of main
    static char[][] gamemenu = {
            {'0','1','0','0','0','0','0','0','0','0'},
            {'0','0','0','0','0','0','0','0','0','0'},
            {'0','0','0','0','@','0','0','0','0','0'},
            {'0','0','0','0','0','0','0','0','0','0'},
            {'0','0','0','0','0','0','0','0','0','0'}
    };
//inside of main
    ArrayList<ArrayList<Character>> e = Utility.CharAToAL(gamemenu);
    Debug.PrintDoubleArray(gamemenu);
    Debug.printLn("\n-------");
    Debug.PrintDoubleArray(e);
    Debug.printLn("\n-------");
    Debug.printLn(String.valueOf(e.get(0).get(1)));

调试只是一个帮助打印值的小脚本。我希望看到的是游戏菜单,但是,它只打印零,如下图所示,上面的破折号是预期的,下面是输出的内容。

Picture of the printout

我认为这可能是由于清除 b 但不清除 b 导致同样的事情一遍又一遍地重复。先感谢您! :>

b不需要在循环外声明

for(int i=0; i < chars.length; i++){
    ArrayList<Character> b = new ArrayList<>();
    for (int k=0; k < chars[i].length; k++){
        Character ch = Character.valueOf(chars[i][k]);
        b.add(ch);
        //this part of code prints out the correct letters
        System.out.println(ch);
     }
    c.add(b);
}

当您执行 clear 时,它会从列表中删除所有内容,并且由于您没有创建新列表,因此之前添加的值也会丢失。