I'm using Stack class for pushing List<Integer>
and popping the item again back from stack:
Stack<List<Integer>> mStack = new Stack<>();
public void pushToStack(View view){
List<Integer> mSearchResults = new ArrayList<>();
for(int i=0; i< 10;i++){
mSearchResults.add(i);
}
Log.d(TAG,"Pushing item: " + mSearchResults.size());
mStack.push(mSearchResults);
Log.d(TAG,"Clearing list");
mSearchResults.clear();
Log.d(TAG,"Size after clearing : " + mSearchResults.size());
}
I'm clearing the list after pushing to stack.
The pushToStack function outputs the logs :
Pushing item: 10
Clearing list
Size after clearing : 0
public void popFromStack(View view){
if(mStack.size() == 0){
Log.d(TAG,"Stack is Empty");
}else{
List<Integer> searchResults = mStack.pop();
Log.d(TAG,"Result size after pop: " + searchResults.size());
}
}
and popFromStack prints the log:
Result size after pop: 0
I wonder why mStack.pop() returns 0 as list item size instead 10.
What am I doing wrong here ?