0

Is there a way that you can use the javascript array.pop() method in java? It would really help if someone finds a way of doing it. I have tried this:

 int neighbor = neighbors.get(neighbors.size()-1);
 neighbors.remove(neighbors.size()-1);

But it doesn't work i'm getting a error:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        at java.util.ArrayList.rangeCheck(Unknown Source)
        at java.util.ArrayList.get(Unknown Source)
SheetJS
  • 22,470
  • 12
  • 65
  • 75

1 Answers1

0

If you use a LinkedList<> instead of ArrayList<>, then you will have access to

addFirst()
addLast()
getFirst()
getLast()
removeFirst()
removeLast()

Which do the same as push and pop in JS. It will also require fewer code changes than Stack<> since ArrayList<> and LinkedList<> both implement List<>, whereas Stack<> is a derivative of Vector<>.

You can view the documentation of LinkedList here.

Slate
  • 3,189
  • 1
  • 31
  • 32