在 Java (Eclipse) 中将一维字符串数组转换为二维字符数组的最佳方法

Best Way to Convert 1D String Array to a 2D Char Array in Java (Eclipse)

对于 Java 中的单词搜索游戏,我要求用户输入任意数量的单词(如果他们想停止添加更多单词,他们将输入一个 'q') ,并且此输入存储在数组列表中,然后将其转换为名为 words. 的一维数组,然后我调用 main 中的一个方法开始游戏。这是代码片段:

System.out.println("Grid of the game");
for(char[] j : letterGrid) {
    System.out.println(j);
  }
   System.out.println("Search for these words...\n");
    for(String j : words) {
      System.out.print(j + ", ");
    }  
    System.out.print("Enter the Word: ");
    String word = br.readLine();
    System.out.print("Enter Row Number: ");
    int row = Integer.parseInt(br.readLine());
     //matching the word using regex
    Pattern pattern = Pattern.compile(word, Pattern.CASE_INSENSITIVE);
     Matcher matcher = pattern.matcher(letterGrid[row-1]);

letterGrid 是一个 2D 字符数组,但是在它写着:Matcher matcher = pattern.matcher(letterGrid[row-1]); 的那一行发生了一个错误,它说:The method matcher(CharSequence) in the type Pattern is not applicable for the arguments. 我试着改变我的程序并将 letterGrid 变成一维字符串数组,它适用于此,但不适用于 2D 字符数组。我用随机字母和用户词填充 letterGrid(水平、垂直或对角线。None 向后)。

我被困在这个问题上,我目前正在寻找解决问题的方法,但我想我也可以在这里提问,因为这里的人提供了很好的意见和建议。任何帮助将不胜感激!

匹配器唯一可接受的参数是字符串 & 将 char 转换为 string 的最快方法是:

char c = 'a';
String s = String.valueOf(c);  

所以,你可以这样做:

String s = String.valueOf(letterGrid[row-1]);
Matcher matcher = pattern.matcher(s);