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. 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.
Because it is an ArrayList characterList.remove(i);
will move everything over to the left one spot, so then the next like 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.
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.
Finally, you should be using .equals()
instead of ==
because the ArrayList returns a Character
object instead of a 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. I tested it with value foo()
and loop did go inside if(characterList.get(i + 1) == closeParenthesis[j])
condition.
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. It should be other way round:
characterList.remove(i + 1);
characterList.remove(i);
Also instead of i=0;
you need to set it to: i = -1;
And more importantly break out of inner for loop by calling break.
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
. if you really want to set 0
for i
again. use i=-1
;
Your for loop increments i
by one AFTER the body of the loop is completed.
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
.
我想您可能已经使用了堆栈数据结构,并且使用了类似于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. You cannot use the == operator to compare two Characters for equality, but must use the .equals() method, as this code demonstrates:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.