When executing this code I get an error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
So in a stack you can never add objects from position n, n=1,2,..., infinity, it will always start at position 0??
My idea was to pila.add("(");
put it in position 0.
public static void main(String[] args){
LinkedList<String> pila=new LinkedList<>();
pila.add(1,"1");
pila.add(2,"2");
pila.add(3,"3");
pila.add(4,"*");
pila.add("(");
System.out.println(pila);
}
According to the class documentation
LinkedList
, the exception is thrown by:IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
. When you createpila
it it is empty so its size is zero (0
), when you try to dopila.add(1,"1");
this saying that in itindex 1
add the value"1"
butindex
it is greater than the size of the stack at that moment, the correct way ispila.add(0,"1");
Now as the behavior of a stack is that the last one in is the first one out so you should always add or remove with index 0
If you want a more specific stack implementation, you can use the Stack class https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
The idea of stacks and queues is to not know the positions of the elements and to use only the element that is available, which is the head.
About your problem, it happens that your list
LinkedList
is empty, so you can't add an element in position 1. First you have to add an element in the starting position. The methodList#insert(int index, E elem)
indicates that such an element must exist.Side note, to work with queues in Java it is better to use the
Queue<E>
. Example:Departure:
To interact with stacks, although a class exists
Stack
, its use is not recommended since it extendsVector
which is a technically deprecated class. Instead, it's better to use the interfaceDeque
and an implementation likeArrayDeque
oLinkedList
:Departure:
I'm afraid the error occurs on the first attempt to add to the collection at a position less than its current size. It cannot be added at position 1 if the list has size 0. What you want can be done perfectly with the class
LinkedList
but you must respect the size of the list when using the index in the insert:The method
add
is overloaded, you must use the appropriate version in each case if you intend to simulate the behavior of a stackYou are trying to add an element to an index that does not exist
Your problem is that when creating the list, its size is 1 node (index 0), but you try to add an element to the second node, which does not exist . In effect, you should start adding elements at index 0.
Now, I see that you are trying to insert at a particular index. That is precisely what a stack and a queue are trying to prevent .
A stack is a LIFO type structure.
LIFO and FIFO type structures do not allow you to add elements at an arbitrary position, but data insertion and extraction are always tied to the first or last element .
They are designed so that you don't have to worry about what order the items are in.
LIFO type structures
LIFO is an acronym for Last In, First Out . Batteries meet this standard.
Think of a pile as a pile of dishes to wash . The first dirty plate you put down stays on the bottom. And when you wash them, that first plate will be the last to come out.
FIFO type structures
FIFO is an acronym for First In, First Out . The queues meet this standard.
You can imagine a supermarket queue . The first person to get to the queue is the first person out.
Regarding your implementation
If you want to implement a stack ( LIFO ), java collections offer a couple of ways:
Use the class
Stack
.This class represents exactly the stack you are looking for. but he has a problem. As it says in the documentation, it inherits from class
Vector
, a class that is little more than forgotten. Also, by inheriting and not implementing, you get inflexible behavior that offers things you don't want, which is not recommended.Use the interface
Deque
and its implementations.Deque
it is pronounced "Deck", meaning "Shuffle" and supports both LIFO and FIFO operations. You could take it as a fusion between queues and stacks .This interface is more modern and no longer extends from
Vector
. It's true that itDeque
offers push and pull at both ends, which might be more functionality than you're looking for, but it also offers flexibility in choosing whether you want a FIFO or LIFO structure.In my opinion, if you want an Exclusively FIFO or LIFO structure , you should use this interface and its implementation
ArrayDeque
.An example stack:
Departure: