Java ArrayList add item outside current size

arraylist, java

Solution

imho the best thing you can do is `items.addAll(Collections.nCopies(6, null))` and hope, that ArrayList implements some behaviour to internally fasten this up

Problem

Wondering whether there is an efficient way to add an item to Java's ArrayList at a bigger position than its current size: Scenario: ``` ArrayList<Item> items = new ArrayList<Item>; ... let's say I add three elements ``` Now I would like to add an item at position 10 (leaving items from 3 to 10 to null) ``` items.add(10,newItem); // item.size() == 3 ``` Is there an efficient way resizing/filling an ArrayList with nulls? Java's implementation makes size field private :-(..

Original source