0

下面是检查匹配括号以查看它们是否正确嵌套的代码。看起来很简单,但我不明白为什么i一旦找到嵌套匹配项就不重置为 0。任何指导将不胜感激。

    String testString = "{}()[] ";
    char [] openParenthesis = {'(','{','['};
    char [] closeParenthesis = {')','}',']'};

    ArrayList<Character> characterList = new ArrayList<Character>();
    for(char c : testString.toCharArray())
    {
        characterList.add(c);
        System.out.println("This is what is added to the list Arraylist: " + c);
    }

    System.out.println();


for(int i = 0; i < characterList.size()-1; i++)
            {
                System.out.println("1st Loop: " +characterList.get(i));
                System.out.println("1st Loop: " + characterList.get(i + 1));
                System.out.println("1st Loop: " + i);
                System.out.println();

                for(int j = 0; j < openParenthesis.length; j++)
                {
                    if (characterList.get(i) == openParenthesis[j])
                    {
                        if(characterList.get(i + 1) == closeParenthesis[j])
                        {
                            System.out.println("Nested Match");
                            System.out.println(characterList.get(i));
                            System.out.println(characterList.get(i + 1));
                            System.out.println();
                            characterList.remove(i);
                            characterList.remove(i + 1);
                            i = 0;
                        }
                    }   
                }
        }
4

6 回答 6

2

首先,您要删除 ArrayList 中的错误位置。

因为它是一个 ArrayListcharacterList.remove(i);会将所有内容移到左侧一个位置,所以下一个喜欢characterList.remove(i+1);删除您想要的右侧的那个。

您还需要在 openParenthesis 循环中添加一个中断,以便在找到匹配项时可以从数组的开头重新开始搜索。

您还需要更改i = 0to,i = -1因为它会在开始 for 循环的下一次迭代之前将其递增到 1。

最后,您应该使用.equals()而不是==因为 ArrayList 返回一个Character对象而不是char.

这是我想出的:

for (int i = 0; i < characterList.size() - 1; i++) {
        System.out.println("1st Loop: " + characterList.get(i));
        System.out.println("1st Loop: " + characterList.get(i + 1));
        System.out.println("1st Loop: " + i);
        System.out.println();

        for (int j = 0; j < openParenthesis.length; j++) {
            if (characterList.get(i).equals(openParenthesis[j])) {
                if (characterList.get(i + 1).equals(closeParenthesis[j])) {
                    System.out.println("Nested Match");
                    System.out.println(characterList.get(i));
                    System.out.println(characterList.get(i + 1));
                    System.out.println();
                    characterList.remove(i);
                    characterList.remove(i);
                    i = -1;
                    break;
                }
            }
        }
    }

此代码已修复上述所有错误,应该可以正常运行。

于 2013-07-17T17:12:37.313 回答
1

那么这取决于你testString是什么。我用值测试了它,foo()并且循环确实进入了内部if(characterList.get(i + 1) == closeParenthesis[j])条件。

但是,您的代码中有一个问题:

characterList.remove(i);
characterList.remove(i + 1);

这将导致删除元素java.lang.IndexOutOfBoundsExceptioni+1位置变得无效(超出范围) 。ith应该反过来:

characterList.remove(i + 1);
characterList.remove(i);

另外,i=0;您不需要将其设置为:i = -1;

更重要的是,通过调用 break 来打破内部 for 循环。

所以你的代码最终应该是这样的:

i = -1;
break;

请参阅工作演示:http: //ideone.com/DfGJ2m

于 2013-07-17T17:08:16.770 回答
1

我猜你可以使用堆栈数据结构并使用类似于http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/的过程

于 2013-07-17T17:16:10.633 回答
1

for显然是因为循环中的 i++i等于1. 如果你真的想重新设置0i使用i=-1

于 2013-07-17T17:09:11.017 回答
1

i在循环主体完成后,您的 for 循环会增加1。

因此,您设置i=0,退出循环体,i++被调用,然后再次使用 进入循环体i ==1

于 2013-07-17T17:13:29.133 回答
0

当您将字符存储在列表中时,java 会将其自动装箱为字符。您不能使用 == 运算符比较两个字符是否相等,但必须使用 .equals() 方法,如以下代码所示:

  Character aOne = new Character('a');
  Character aTwo = new Character('a');

  if(aOne == aTwo)
  {
     System.out.println("aOne and aTwo == operator says they are equal");
  }
  else
  {
     System.out.println("aOne and aTwo == operator says they are NOT equal");
  }

  if(aOne.equals(aTwo))
  {
     System.out.println("aOne and aTwo .equals operator says they are equal");
  }

代码打印出来:

aOne and aTwo == operator says they are NOT equal
aOne and aTwo .equals operator says they are equal
于 2013-07-17T17:12:23.670 回答