0

If I use list.add(int, int) method, it will throw an IndexOutBoundException if the position is bigger than or equals to list.size().

I would like to have something that increases the size by adding null elements (if necessairy) until it can safely add a real element to the end.

So lets say I have a list(list.size()= 0), I want to add 5 to the third position, then I would get a list of where the size equals to 4, and the elements are null, null, null, 5.

Dimitri Dewaele
  • 9,061
  • 17
  • 67
  • 116
Sanyifejű
  • 2,240
  • 8
  • 39
  • 62

2 Answers2

0

Try using ensureCapacity(int minCapacity) method of ArrayList

Zeeshan
  • 9,465
  • 14
  • 65
  • 93
  • 1
    no way. that is completely different – Sanyifejű Feb 19 '14 at 09:59
  • 1
    `ensureCapacity(int)` doesn't change the actual size of the list, it simply allocates memory for a certain size the list can reach before it next needs to copy values. It just saves on incremental reallocation. Moreover, it is an `ArrayList` method, not applicable to any other implementation of `java.util.List`. – Chthonic Project Feb 19 '14 at 10:11
  • My bad :(. Got confused with the method description: _Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument_. – Zeeshan Feb 19 '14 at 10:14
0
    You can do this by adding null elements in the List like:


        import java util.List;
        import java.util.ArrayList;

        public class Hello {
         public static void main(String args[]){
        List<Integer> list = new ArrayList<Integer>();
        list.add(0, null);
        list.add(1, null);
        list.add(2, null);
        list.add(3, 5);

        System.out.println(list);
        }
        }

Now, you will an output [null, null, null, 5]. I think this might serve your purpose.

Or you can use this way:

List list = Collections.nCopies(5,null);
ArrayList<Integer> aList = new ArrayList<Integer>(list);
aList.add(5,5);
System.out.println(aList);

You will get an output as : [null, null, null, null, null, 5]

If this could serve your prupose.
Sambhav
  • 208
  • 2
  • 16
  • Yes, I can do it, but that is what I wanted to avoid and find a more elegant, out-of-box solution if there is any. – Sanyifejű Feb 19 '14 at 10:19
  • Actually, ArrayList class uses RangeCheck(int index) which private which checks the index with size and returns exception when index>size. So, it won't be possible to add value directly to an index 3 without having values in 0, 1 and 2. – Sambhav Feb 19 '14 at 10:45
  • You can use a loop to add null values before your desired index. – Sambhav Feb 19 '14 at 10:48
  • Maybe I did not express myself clearly enough in the question. I do not really want to do loops, or manual insertions. I know only a limited lists' APIs (Linkedlist, ArrayList etc..), and none of those implementation provide the functionality I need. So I thought maybe there is "FancyUnkownList" out there, that I do not yet know but the community does, with out-of-box functionality. – Sanyifejű Feb 19 '14 at 11:12