I have no trouble with my code on working part but I still have some worries if there is something missing so I want a review for my code. Also, I wonder if the exceptions are true type for these methods.public class ArrayListStack<E> implements Stack<E> {    private ArrayList<E> list;    public ArrayListStack()    {        list = new ArrayList<>();    }    public int size()    {        return list.size();    }    public boolean isEmpty()    {        return size() == 0;    }    public void push(E e)    {        list.add(size(), e);    }    public E top()    {        if(isEmpty()) throw new IllegalStateException(Array is empty);        return list.get(size()-1);    }    public E pop()    {        if(isEmpty()) throw new IllegalStateException(Array is empty);        return list.remove(size()-1);    }}
Linked List Stack
java;array;stack
Error messages should not leak implementation details unnecessarilyArrayListStack is an implementation of your custom interface Stack.Presumably users would declare their stacks using the interface type, for example:Stack<String> strings = new ArrayListStack<>();The stack of strings may get passed to methods expecting a Stack<String>,and then if top or pop get called when the stack is empty,the exception message will say Array is empty,which is an implementation detail.Instead of throwing new IllegalStateException(Array is empty),you could do what the JDK implementation of Stack does,and throw EmptyStackException.No need for a custom message,it's already encapsulated in that exception.You could get some other hints from the JDK's implementation,such as the method names and return types.Use final when possibleThe list field can be marked final.It's good to mark fields final when possible,as these fields cannot be reassigned,they are easier to read, understand, and trust.You could also initialize the field on a single line,and drop the explicit constructor:private final List<E> list = new ArrayList<>();Don't repeat yourselfThis line appears twice in the code:if (isEmpty()) throw new IllegalStateException(Array is empty);You could move this to a helper function, you could call it ensureNonEmptyStack.Appending to a listThe 2-parameter version of list.add inserts an element at the specified position.In your code, your intention is to append at the end of the list.That's what the 1-parameter version of list.add does,and it would be simpler and more appropriate in this code.Formatting conventions in JavaThe convention is to place opening braces on the same line as the statements.