将字符串数组与嵌套循环进行比较时出现问题

时间:2021-01-04 21:17:09

This is the problem I am trying to solve: I have two arrays of Strings ("matches" and "visibleObjects"). I would like to search through all the words in the array "matches" to see if there is at least one word from the array "visibleObjects". If this condition is satisfied, I'd like to search through the words in "matches" again this time looking for at least a word from the array "actionWords". This is what I have, where "testDir" is just a debug string that gets printed:

这是我试图解决的问题:我有两个字符串数组(“匹配”和“visibleObjects”)。我想搜索数组“匹配”中的所有单词,以查看数组“visibleObjects”中是否至少有一个单词。如果满足这个条件,我想再次搜索“匹配”中的单词,这样就可以从数组“actionWords”中查找至少一个单词。这就是我所拥有的,其中“testDir”只是一个打印的调试字符串:

protected void Action(){
        boolean actionWord = false;
        String target = null;

        testDir = "first stage";
        firstLoop:
        for(String word : matches)
        {
            testDir += " " + word;
            for(String hint : visibleObjects)
            {
                testDir += " " + hint;
                if(word.equals(hint))
                {
                    target = word; //found a matching word
                    testDir = "Hint found";
                    break firstLoop;
                }
            }
        }

        if(target != null)
        {
            testDir = "stage two";

            secondLoop:
            for(String word : matches)
            {
                for(String action : actionWords)
                {
                    if(word.equals(action))
                    {
                        actionWord = true; //found one word from the actionWords array
                        testDir = "Acion OK";
                        break secondLoop;
                    }
                }
            }
        }


        if(actionWord){
            testDir = target;
            performAction(target);
        } 
    }

All I get printed is the first word from the array matches and all the words from the array visibleObject once, so it doesnt get past the second loop....

我得到的所有内容都是数组匹配中的第一个单词和数组visibleObject中的所有单词,因此它不会超过第二个循环....

Is this code right? Can anyone spot the bug?

这段代码对吗?谁能发现这个bug?

Thanks for your help!

谢谢你的帮助!

2 个解决方案

#1


0  

The code seems to work fine for me:

代码似乎对我很好:

public static void main(String[] args)
{
    String[] matches = { "a", "b", "c" };
    String[] visibleObjects = { "c", "d", "e" };
    String target = null;

    firstLoop: for (String word : matches)
    {
        for (String hint : visibleObjects)
        {
            if (word.equals(hint))
            {
                target = word;
                break firstLoop;
            }
        }
    }

    System.out.println(target);
}

This prints out c. If you have no match it would print null.

这打印出c。如果没有匹配则会打印null。

Note that you could also use one loop and the List.contains(...) method, like this

请注意,您也可以使用一个循环和List.contains(...)方法,如下所示

List<String> l = Arrays.asList(visbleObjects)

for (String word : matches)
{
    if (l.contains(word))
    {
          target = word;
          break;
    }
}

#2


0  

You stop the outer loop on the first match (break firstLoop;) - and I assume that is not what you want, do you?

你在第一场比赛中停止外循环(break firstLoop;) - 我认为这不是你想要的,是吗?

Instead do one of the following:

而是执行以下操作之一:

  1. continue the outer loop instead of stopping it (continue firstLoop;)
  2. 继续外循环而不是停止它(继续firstLoop;)

  3. break the inner loop only (break;)
  4. 只打破内循环(break;)

#1


0  

The code seems to work fine for me:

代码似乎对我很好:

public static void main(String[] args)
{
    String[] matches = { "a", "b", "c" };
    String[] visibleObjects = { "c", "d", "e" };
    String target = null;

    firstLoop: for (String word : matches)
    {
        for (String hint : visibleObjects)
        {
            if (word.equals(hint))
            {
                target = word;
                break firstLoop;
            }
        }
    }

    System.out.println(target);
}

This prints out c. If you have no match it would print null.

这打印出c。如果没有匹配则会打印null。

Note that you could also use one loop and the List.contains(...) method, like this

请注意,您也可以使用一个循环和List.contains(...)方法,如下所示

List<String> l = Arrays.asList(visbleObjects)

for (String word : matches)
{
    if (l.contains(word))
    {
          target = word;
          break;
    }
}

#2


0  

You stop the outer loop on the first match (break firstLoop;) - and I assume that is not what you want, do you?

你在第一场比赛中停止外循环(break firstLoop;) - 我认为这不是你想要的,是吗?

Instead do one of the following:

而是执行以下操作之一:

  1. continue the outer loop instead of stopping it (continue firstLoop;)
  2. 继续外循环而不是停止它(继续firstLoop;)

  3. break the inner loop only (break;)
  4. 只打破内循环(break;)