List<Integer> stack1 = new ArrayList<>();
List<Integer> stack2 = new ArrayList<>();
//some add operation
......
//some add operation
if(stack1.remove(index1) == stack2.get(index2-1))
stack2.remove(--index2);
The code above works wrong. While the code below works right.
List<Integer> stack1 = new ArrayList<>();
List<Integer> stack2 = new ArrayList<>();
//some add operation
......
//some add operation
int i = stack1.remove(index1);
int j = stack2.get(index2-1);
if(i == j)
stack2.remove(--index2);
The former code works that even if the 'if' sentence in latter code judges true, it judges false, which makes me confused.
sentence,judges... I suggest reading through the Oracle Java guide. It's pretty comprehensive and will explain everything you need.