Page 1 of 1

About Question enthuware.ocajp.i.v7.2.851 :

Posted: Wed Nov 12, 2014 10:28 am
by Kevin_C
I had the answer correct, but I was wondering about the answer:
ArrayList cannot resize dynamically if you add more number of elements than its capacity.

ArrayList also got a max-sized constructor, right?

Code: Select all

ArrayList<Integer> l = new ArrayList<>(5); // <- ArrayList of size 5 (array behind the scenes with length 5)

// So I can't do:
for(int i = 0; i < 6; i++)
      l.add(i); // For element 6 it will throw an IndexOutOfBoundsException (or ArrayIndexOutOfBoundsException?)
The only reason I can see when you want a max-sized ArrayList is when you don't want default values like with a max-sized array. Probably the reason why it's added. :)

Re: About Question enthuware.ocajp.i.v7.2.851 :

Posted: Fri Feb 20, 2015 8:37 pm
by Harini
Hi,

for this question, one of the options says "You cannot store primitives in an ArrayList."

If my understanding is correct, primitives are int, short , long etc. And am able to add them to an ArrayList. So how does the above statement hold true ?

Re: About Question enthuware.ocajp.i.v7.2.851 :

Posted: Fri Feb 20, 2015 10:00 pm
by admin
Primitives are actually wrapped into wrapper objects and those wrapper objects (not primitives) are stored in the list.

Re: About Question enthuware.ocajp.i.v7.2.851 :

Posted: Mon Oct 17, 2016 11:02 am
by sahilsaid
I think the following option needs to be changed to make things clear.

"You cannot store primitives in an ArrayList."

Should be changed to something similar.

"You cannot store primitives directly (or as it is) in an ArrayList."

I though the option is saying that it is impossible to store the primitives in the arrayList.

Re: About Question enthuware.ocajp.i.v7.2.851 :

Posted: Mon Oct 17, 2016 11:37 am
by admin
It IS impossible to store primitives in a List. Wrappers are not primitives.

Re: About Question enthuware.ocajp.i.v7.2.851 :

Posted: Mon Oct 17, 2016 11:59 am
by sahilsaid
I am not saying that primitives are wrappers but that option made me think that it is asking whether the following is possible or not in arraylist.

List a = new ArrayList();
a.add(1); //compile error according to that option maybe? but works fine of course.

It was only a suggestion from me to improve the option.

Re: About Question enthuware.ocajp.i.v7.2.851 :

Posted: Mon Oct 17, 2016 12:09 pm
by admin
Ok, I see your point. Will make it more clear.
thank you for your feedback.
-Paul.