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]

If you want that behaviour:

List<T> snake = new ArrayList<>(){
    @Override
    public T get(int i) {
        return super.get((i + size()) % size());
   }
};

This will work for any value of i.

Leave a Comment