First, Java is not Python (although Jython implements Python in Java). Second, you should read the JavaDoc on ArrayList – that is it throws an Exception
,
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
Finally, you can do this
myList.get(myList.size() - 1);
to get this last element in your List
(e.g. myList
).
solved why is it that we can’t use ArrayList.get(-1) to get the last element of the ArrayList in java? it works in python though [closed]