下面是检查匹配括号以查看它们是否正确嵌套的代码。看起来很简单,但我不明白为什么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;
}
}
}
}