I have an assignment in my class to implement something in Java and Python. I need to implement an IntegerStack with both languages. All the values are supposed to be held in an array and there are some meta data values like head() index.
When I implement this is Java I just create an Array with max size (that I choose):
public class IntegerStack {
public static int MAX_NUMBER = 50;
private int[] _stack;
private int _head;
public IntegerStack() {
_stack = new int[MAX_NUMBER];
_head = -1;
}
public boolean emptyStack() {
return _head < 0;
}
public int head() {
if (_head < 0)
throw new StackOverflowError("The stack is empty."); // underflow
return _stack[_head];
}
// [...]
}
I'm really not sure how to do this in Python. I checked out a couple tutorials, where they all say that an array in python has the syntax my_array = [1,2,3]
. But it's different, because I can use it as a List and append items as I want. So I could make a for loop and initiate 50 zero elements into a Python array, but would it be the same thing as in Java? It is not clear to me how a Python List is different from an Array.
list
is similar to JavaArrayList
. There is nothing in Python that is equivalent to Java array (thoughlist
is still the closest).