Reputation: 125
Why are the following statements equivalent? emptyRow and emptyCol are Integer stacks, and Grid is a two dimensional array of integers. There is no need for you to know what the program does, but in case you're wondering it's a sudoku puzzle solving algorithm that uses basic back-tracking.
Both statements allow the program to run without any bugs, but I don't understand how this is possible since .pop() returns the top most integer in the first implementation, while the second one first pops the top most items and retrieves the integers directly under those.
Statement A:
Grid[emptyRow.pop()][emptyCol.pop()] = 0;
Statement B:
emptyRow.pop();
emptyCol.pop();
Grid[emptyRow.peek()][emptyCol.peek()] = 0;
Sorry if this is a silly logic problem with my code, I'm just checking to see if there is something I don't know about how stack operations work.
Upvotes: 0
Views: 56
Reputation: 533660
Statement A is the same as
Grid[emptyRow.peek()][emptyCol.peek()] = 0;
emptyRow.pop();
emptyCol.pop();
I think you have the order confused, and perhaps in your use case it doesn't matter but the code is not the same.
Upvotes: 1