I create a stack class with the name StackOfBook
. I need 2 stacks so I create this on my main class:
StackOfBook stack1 = new StackOfBook();
while (true) {
long number = sc.nextLong();
if (number == -1) {
break;
}
stack1.push(number);
}
StackOfBook stack2 = new StackOfBook(stack1.getTop());
This is my stack class:
private int top = 0;
private ArrayList<Long> arrBook;
public StackOfBook(){
arrBook = new ArrayList<Long>();
}
public StackOfBook(int size){
arrBook= new arrBook<Long>(size);
}
public int getTop() {
return this.top;
}
I tried to initialize the ArrayList
in the second stack without changing its top, but when I tried this the ArrayList
in my stack2 doesn't initialize with stack1.getTop()
. I have checked the variable size in the constructor and it has a value. Why is it wrong?