[英]Loop is not resetting?
Belowe is code that checks for matching parentheses to see if they are nested properly. 下面的代码检查匹配的括号以查看它们是否正确嵌套。 It seems really simple, but I don't understand why
i
does not reset to 0 once a nested match is found. 看起来真的很简单,但是我不明白为什么一旦找到嵌套匹配项后
i
不重置为0。 Any guidance would be much appreciated. 任何指导将不胜感激。
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;
}
}
}
}
First off all you are removing the wrong spots in the ArrayList. 首先,您要删除ArrayList中的错误斑点。
Because it is an ArrayList characterList.remove(i);
因为它是一个ArrayList
characterList.remove(i);
will move everything over to the left one spot, so then the next like characterList.remove(i+1);
会将所有内容移到左侧一个位置,因此将下一个位置类似于
characterList.remove(i+1);
removes the one to the right of where you want. 删除您想要的位置右侧的一个。
You also need to add a break in the openParenthesis loop so you can start the search over at the beginning of the array if you find a match. 您还需要在openParenthesis循环中添加一个中断,以便在找到匹配项的情况下可以从数组的开头开始搜索。
You also need to change the i = 0
to i = -1
because it increments it to one BEFORE it starts the next iteration of the for loop. 您还需要将
i = 0
更改为i = -1
因为在开始for循环的下一次迭代之前,它将i = 0
递增。
Finally, you should be using .equals()
instead of ==
because the ArrayList returns a Character
object instead of a char
. 最后,您应该使用
.equals()
而不是==
因为ArrayList返回一个Character
对象而不是char
。
Here is what I came up with: 这是我想出的:
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;
}
}
}
}
This code has fixed all the errors mentioned above and should run correctly. 此代码修复了上面提到的所有错误,应该可以正确运行。
Well it depends on what your testString
is. 好吧,这取决于您的
testString
是什么。 I tested it with value foo()
and loop did go inside if(characterList.get(i + 1) == closeParenthesis[j])
condition. 我用值
foo()
测试了它,并且循环确实进入了if(characterList.get(i + 1) == closeParenthesis[j])
条件下。
However there is a problem in your code where you have: 但是,您的代码中存在一个问题:
characterList.remove(i);
characterList.remove(i + 1);
Which will result in java.lang.IndexOutOfBoundsException
since i+1
location has become invalid (out of range) after deleting ith
element. 这将导致
java.lang.IndexOutOfBoundsException
因为删除ith
元素后i+1
位置已变为无效(超出范围)。 It should be other way round: 应该是相反的方式:
characterList.remove(i + 1);
characterList.remove(i);
Also instead of i=0;
也代替
i=0;
you need to set it to: i = -1;
您需要将其设置为:
i = -1;
And more importantly break out of inner for loop by calling break. 更重要的是,通过调用break可以打破for循环的内在循环。
So your code in the end should look like this: 因此,您的代码最后应如下所示:
i = -1;
break;
Obviously because of the i++ in for
loop i
equal to 1
. 第i ++中的显然是因为
for
循环i
等于1
。 if you really want to set 0
for i
again. 如果您真的想再次为
i
设置0
。 use i=-1
; 使用
i=-1
;
Your for loop increments i
by one AFTER the body of the loop is completed. 循环体完成后,for循环将
i
递增1。
Thus, you set i=0
, you exit the body of the loop, i++
is called, and you enter the loop body once more with i ==1
. 因此,您设置
i=0
,退出循环的主体,调用i++
,然后使用i ==1
再次进入循环主体。
我想您可能已经使用了堆栈数据结构,并且使用了类似于http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/的过程
When you store a character in a list, java autoboxes it to a Character. 当您将字符存储在列表中时,java会将其自动装箱为一个字符。 You cannot use the == operator to compare two Characters for equality, but must use the .equals() method, as this code demonstrates:
您不能使用==运算符比较两个字符是否相等,但必须使用.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");
}
Code prints out: 代码输出:
aOne and aTwo == operator says they are NOT equal
aOne and aTwo .equals operator says they are equal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.