The variable
stack is local to the
main method - meaning it's defined there and is therefore only known there. If you want to use it in the
printStack method you need to pass it there.
Note that in the loop above the call to
printStack you
pop all the values off of
stack. If you then pass
stack to it,
stack will be empty.
Also, you say the for loop iterates over the array. It does not. It iterates over
stack. What's in
stack has no connection to the array other than that you push the same values in it as were initialized into the array. Instead you should iterate the array to fill
stack, like so:
for (int i = 0; i < intArray.length; i++) {
stack.push(intArray[i]);
}
Now
stack will have the same values to which
intArray is initialized, whatever their values or order may be.