-3

I was writing something that needs an arrayList of size n, so I did the following:

List<Set<Integer>> list = new ArrayList<Set<Integer>(n);

And when I was trying to access an element of the list e.g.

list.get(someValue that is <n)

I got arrayList out of bound exception, so I guess putting a n there doesn't really help you initialize the list, but just pre-allocate the space.

Is there a way to do the initialization after which there are actually null or objects in each slot?

I end up using a for loop and adding n empty set and then index into the list.

Is there a better way TO INITIALIZE AN ARRAYLIST IF THE SIZE IS KNOWN IN ADVANCE?

Please know what I'm asking before saying this is a duplicate.

Hope my question is clear.

Thanks in advance.

Some of you think what I tried to do is meaningless. This happens when I tried to solve a bucket sort related problem where the index of the set I tried to access in the array is known. So for example, I want to add some elements to the set at position 1, 3, 2, 4... then it would be convenient if I can just get the set at position 1, 3, 2, 4...

ljzhanglc
  • 507
  • 2
  • 8
  • 11
  • 1
    Mostly there is not know way to intialize. Why on earth anyone wants to do that. – Panther Jan 05 '18 at 05:12
  • Java tries to give you a chance for freedom, but you still insist on staying in the prison. – Soner from The Ottoman Empire Jan 05 '18 at 05:16
  • 1
    The parameter `n` you have specified is the initial capacity and not the size as you might be expecting. – VPK Jan 05 '18 at 05:17
  • As VPK mentions, when I run the code in eclipse it says that the size of the arraylist is 0, despite initializing it that way. Panther also brings up a good point: there's no reason to initialize the arraylist because the whole point of it is to grow larger at will. – Meepo Jan 05 '18 at 05:21
  • 1
    How about `new ArrayList<>(Collections.nCopies(n, null));`? As for why you would want to do this, I have no idea... – bcsb1001 Jan 05 '18 at 06:01
  • Possible duplicate of [Initial size for the ArrayList](https://stackoverflow.com/questions/8896758/initial-size-for-the-arraylist) – tkruse Jan 05 '18 at 06:09
  • 1
    Plus my question is not the same as the one mentioned above, I am asking a way to directly index into an arrayList after initialization if possible. – ljzhanglc Jan 05 '18 at 20:14
  • @Panther I want to get the set at position 3 directly, the sets are buckets of numbers and I know which bucket to put new number in, this is why. May be you haven't come across situations like this but I have and was wondering if there is a better way to do it instead of a for loop in front. – ljzhanglc Jan 05 '18 at 20:18
  • @snr Because you can't create an array of sets so I used ArrayList. Knowing the size in advance == staying in the prison??? – ljzhanglc Jan 05 '18 at 20:19
  • Don't understand why people downvoting this question. its valid question and not a duplicate. – Panther Jan 08 '18 at 05:24
  • @LingjunZhang Read this https://meta.stackexchange.com/a/5235 – Ravi Jan 09 '18 at 15:33
  • @Panther Thank you soooooo much. People just like seeing something that looks similar and would just down vote and say it's a duplicate without truly understand what I was asking. Or is it because I a not clear about my question. Well I tried, and I think whoever takes some time read my question will understand I'm not asking about the arrayList size – ljzhanglc Jan 10 '18 at 00:31
  • @Ravi I know how to choose an answer. Thank you. – ljzhanglc Jan 11 '18 at 04:17
  • @LingjunZhang then what is the reason you haven't selected any answer yet ??? neither you have replied to any answer – Ravi Jan 11 '18 at 04:19
  • @Ravi I have replied to both, thank you. As you have mentioned yourself, you are not answering what I was asking for. So thank you for trying to help. I get the difference between capacity and size, but I have also clearly stated my question above. I won't repeat it here again. And so far, the right answer is probably: there isn't a way. So I should just use a loop to do the initialization and put n empty sets in. – ljzhanglc Jan 12 '18 at 18:20
  • Just got here from searching for this problem. I have to say the responses here are just bizarre. Wanting to initialize an array == "you still insist on staying in the prison" ?? Yikes. OP, I feel your pain. – Robert Dodier Jul 17 '20 at 16:37

2 Answers2

2

If you take a look at Java API documentation, you will see that there are 3 constructors provided for ArrayList:

ArrayList()

ArrayList(Collection c) - Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(int initialCapacity) - Constructs an empty list with the specified initial capacity.

You can use second of listed constructors if you need to fill ArrayList's slots in the place of its definition. You need to be aware that there needs to be passed a Collection, so you can do something like that, for example:

ArrayList<Integer> al = new ArrayList<>(Arrays.asList(1,2,3,4,5);

Now size() of al is 5 and it is filled with numbers 1,2,3,4 and 5.

Also, you need to be aware that ArrayList doesn't work like Arrays and it's size is not fixed - it changes due to adding or removing items.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • Thanks for your help! I know how ArrayList works, I guess there isn't a way to do what I was hoping to do, but it's good knowing that there isn't. I should just initial my list using a for loop and put n empty sets in. – ljzhanglc Jan 12 '18 at 18:12
  • @LingjunZhang You might want to have a look at comments under Ravi's answer. Maybe this could help solve your problem in one way or another. Btw, if you think that some answers were helpful, you might want to consider upvoting them and if you find that answer covers all what you were asking for, consider ticking it as the accepted one. – Przemysław Moskal Jan 12 '18 at 19:29
  • Moskal Sorry I can't up vote answers for my low reputations. The system doesn't allow it. – ljzhanglc Jan 13 '18 at 18:53
  • @LingjunZhang It's okay, you can vote when you gain reputation of 15 points actually. Did you have a look at what I've written under Ravi's answer? This might be helpful in your case, tell me what you think about it :-) – Przemysław Moskal Jan 13 '18 at 18:56
2

I'm taking the liberty of repeating bcsb1001's solution, which is perhaps obscured by various comments. In order to initialize an array of a fixed size with valid placeholder values:

new ArrayList (Collections.nCopies (n, null));

This idea was the best I could find in various searches, maybe it will work for others too.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48