开发者

Problem comparing arrays of Strings with nested loops

开发者 https://www.devze.com 2023-04-07 04:18 出处:网络
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

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:

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....

Is this code right? Can anyone spot the bug?

Thanks for your help!


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.

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

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

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


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

Instead do one of the following:

  1. continue the outer loop instead of stopping it (continue firstLoop;)
  2. break the inner loop only (break;)
0

精彩评论

暂无评论...
验证码 换一张
取 消