繁体   English   中英

如何比较字符串数组和字符数组

[英]How do I compare a a string array with a char array

我很拼命,我需要你的帮助! 我正在研究一个填字游戏解谜器,其中将难题和单词提供给了我们,我们需要格式化难题,以便数组中的每个索引都有自己的字母。 现在我把拼图索引和单词分开了,所以我可以逐个字母地比较它们,但是有更好的方法吗? 可以说,是否有一个包含我要查找的单词的字符串数组,并且有一个字符数组可以找到该单词?

    My array looks like this: 
    [W, V, E, R, T, I, C, A, L, L]
    [R, O, O, A, F, F, L, S, A, B]
    [A, C, R, I, L, I, A, T, O, A]
    [N, D, O, D, K, O, N, W, D, C]
    [D, R, K, E, S, O, O, D, D, K]
    [O, E, E, P, Z, E, G, L, I, W]
    [M, S, I, I, H, O, A, E, R, A]
    [A, L, R, K, R, R, I, R, E, R]
    [K, O, D, I, D, E, D, R, C, D]
    [H, E, L, W, S, L, E, U, T, H]

并说我在寻找“垂直”。 如何设置循环以查找组成字符串“ Vertical”的字符。 还是我必须逐字母比较它?

这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class WordSearch {

    public static void main(String[] args) throws Exception{
            File file = new File("puzzle.txt"); 
            Scanner sc = new Scanner(file);
        int row = sc.nextInt();
        int col = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the puzzle
        char[][] puzzle = new char[row][col];   


         //Read in the strings from the file into the array.
        for (int i=0; i<row; i++){
            String getChar = (new String(sc.next()));
            for(int j = 0;j<col; j++){
                puzzle[i][j] = getChar.charAt(j);
                }
            }

        //Test Print
         for (int i=0; i<puzzle.length; i++){

               System.out.print(Arrays.toString(puzzle[i]));
               System.out.println("");
         }



        //Read the number of words and move to the next line    
        int numwords = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the words and read in the words from a file
        String[] words = new String[numwords];
        for(int i=0; i<numwords; i++){
            words[i] = sc.nextLine();
        }

        //look for each word
        for(int i=0; i<numwords; i++){
            String currentword = words[i];      

            String[] strings1 = currentword.split("");

            int range = currentword.length()+1;

            String[] strings2 = Arrays.copyOfRange(strings1, 1, range);

            int range2 = strings2.length;
            char[] charwords = new char[range2];    





            }



    //Close the scanner     
    sc.close();
    }

    }

这项工作是否可以测试对角线:

private boolean checkDiagonals(int row, int col, String word, char[][] puzzle) {
        //Checking diagonals direction
        for(int letter = 1; letter < word.length(); letter++) {
            if(puzzle[row + letter][col + letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row + letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
                return false;
            }
        }
        return true;
    }

可以说,是否有一个包含我要查找的单词的字符串数组,并且有一个字符数组可以找到该单词?

是的,只需

从String数组中获取元素,例如

  String arrayElem = str[i]; // i is index.

这给你一个字符串。

然后

  boolean result=Arrays.equals(arrayElem.toCharArray(), actualCharArray);

因为单词可以在任何方向上显示,所以简单地逐字母进行比较会更有意义。 在整个字符数组中搜索要查找的单词的第一个字母。 例如,如果您要查找“垂直”,则将遍历整个数组以查找字母“ V”。

然后具有一个功能,可以验证所有方向(上,下,左,右,对角线)的单词。 我们称它为check(int row, int col)

for(int row = 0; row < puzzle.length; row++) {
    for(int col = 0; col < puzzle[0].length; col++) {
        if(puzzle[row][col] == word.charAt(0))
            if(check(row, col) == true)
                    //We found it.
    }
}

然后编写check(int row, int col, String word, char[][] puzzle) ,您将检查每个基本方向,例如,如果您检查的正确,则将继续向右进行比较并将字母与该单词,直到您与该单词匹配或发现不一致为止。 然后,如果没有方向可用,则返回false。

例如,这里向右进行检查(如果我们不在数组中,则要执行此操作,则不会进行错误检查)。

private boolean checkRight(int row, int col, String word, char[][] puzzle) {
    //Checking right direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}

然后,您需要为每个方向选择一个,然后在检查功能中,它看起来像

public boolean check(int row, int col, String word, char[][] puzzle) {
    if(checkRight(row, col, word, puzzle)) return true;
    if(checkLeft(row, col, word, puzzle)) return true;
    if(checkUp(row, col, word, puzzle)) return true;
    if(checkDown(row, col, word, puzzle)) return true;
    if(checkDiagonals(row, col, word, puzzle)) return true;
    return false;
}

我建议将数组中的每一行都转换为String然后使用String.indexOf() 这将确定您要查找的字符串是否存在于行中以及它从什么位置开始。 如果返回-1 ,则说明该行中不存在该字符串。

String.indexOf()

暂无
暂无

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

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