简体   繁体   English

如何一次将一个字符输入一个字符串到二维数组中

[英]How to input a string one charecter at a time into a 2d array

So i have a string and i need to put it into a 2d array this is what i have so far. 所以我有一个字符串,我需要将其放入2d数组中,这是我到目前为止所拥有的。

mazeString = ".............."    
char[][] mazeArray = new char [50][30];
         for (int i = 0; i < (height*2)-1; i++){
           for (int j = 0; j < (width*2)-1; j++){
              mazeArray[j][i] = mazeString.next();
           }

         }

Like said in the comments, mazeString is declared and initialized as a String Literal. 如评论中所述, mazeString被声明并初始化为String Literal。 All .next() methods are reserved for Scanner objects. 所有.next()方法都保留用于Scanner对象。 Java doesn't let you use scan Java不允许您使用扫描

So i figured it out. 所以我想通了。

while (input.hasNextLine()) {
      mazefile = mazefile + input.nextLine();
    }


for (int i = 0; i < (height*2)+1; i++){
       for (int j = 0; j < (width*2)+1; j++){
          mazeArray[i][j] = mazefile.charAt(counter);
          counter++;
       }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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