Timeline for Why does java.util.ArrayList allow to add null?
Current License: CC BY-SA 3.0
18 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 26, 2019 at 14:39 | review | Suggested edits | |||
Nov 26, 2019 at 15:12 | |||||
Sep 26, 2019 at 9:35 | comment | added | gnat | many (almost all) of the prior comments were useful and I addressed all of them. The only comment I didn't address is the last one and this is because I couldn't make any sense of it no matter how I tried | |
Sep 23, 2015 at 18:33 | comment | added | Biman Tripathy | very irrelevant example presented. First of all, who told u that arraylist.set(0,null) throws Runtime Ex because you are trying to set a null value at an index? Runtime Ex is thrown because set() works only for existing indexes, and you may have tried to set on an arraylist which doesn't have any value at index 0 yet. add() and set() are different methods, and set() cannot substitute add() functionality. In case of arrays, assignments are ambiguous, means it doesn't matter you are adding a new value or overwriting existing one. The example provided has no relevance to the question. | |
Jan 19, 2015 at 8:46 | history | edited | gnat | CC BY-SA 3.0 |
updated per discussion in comments
|
Oct 9, 2014 at 12:56 | comment | added | gnat |
@AndresF. re-read the very roundup you refer, it makes a pretty strong connection with arrays with the explanation that null is "unavoidable in object arrays". You may call it guesswork as much as you want, but in a collection intended by name to resemble API user behavior of plain arrays, keeping the similarity in treating nulls perfectly fits within principle of least astonishment
|
|
Oct 9, 2014 at 12:37 | comment | added | Andres F. | This answer is plain wrong. This is just unsupported guesswork about nulls being allowed in a list implementation. Here is a roundup of Java collections allowing/disallowing nulls; notice how it has nothing to do with similarity to arrays. | |
Sep 17, 2014 at 7:05 | comment | added | gnat | @user949300 correct. I couldn't make a compelling general case for wide usage of null being the reason (if memory serves this was because API docs allow collections rejecting null, and do so without any reservations). Nor could I find that this was the case for ArrayList | |
Sep 16, 2014 at 15:40 | comment | added | user949300 | @gnat - A quick Google search finds many articles about nulls, including this quote: "A wise man once said you are not a real Java programmer until you've dealt with a null pointer exception. Joking aside, the null reference is the source of many problems." It is undeniable that nulls are commonly used /misused in Java. Or do you want evidence that the designers of Collections specifically allowed nulls because they were common? Can't provide that directly, but I believe they designers of Collections were smart, and might have avoided nulls if they were not so darned common. | |
Sep 16, 2014 at 13:37 | comment | added | gnat | @user949300 nice theory about wide usage of null being the reason, but I guess you didn't post it as an answer because you found nothing to back it up? FWIW I also considered this when doing research before answering, and had to drop exactly for this reason, found nothing to back it up | |
Sep 16, 2014 at 6:52 | comment | added | gnat | @FlorianF exposing underlying data structure in the name (when done right) sets right expectations of API users. Using your own example, knowing that mapping goes to tree helps to understand "guaranteed log(n) time cost for the containsKey, get, put and remove operations" (javadocs) | |
Sep 15, 2014 at 18:13 | comment | added | user949300 | This answer, IMO, is just plain wrong. Nulls are allowed because in Java, for better or worse, (IMO, worse) null gets used a lot to represent uninitialized or missing values. How did it get the most votes and the "accept" check? Seriously, I'm really losing faith in StackOverflow these days. | |
Sep 14, 2014 at 6:38 | comment | added | Florian F | ArrayList isn't called like that because it mimics an array. It is called like that because it is a list implemented as an array. Just as a TreeMap doesn't behave like a Tree. | |
Jul 21, 2014 at 22:48 | comment | added | Stephen C |
Yea ... but a simpler and (IMO) more plausible explanation is that allowing null entries is useful in a lot of cases.
|
|
Jul 21, 2014 at 1:50 | comment | added | gnat |
@StephenC LinkedList could get this "transitively" so to speak, once designers decided that they want null entries to be supported by particular List implementation, they could push this to other implementations as a matter of consistency. Rephrasing self, "API user knowing that arrayList.set(0, null) works OK, would be quite surprised to find out if similar code for LinkedList would throw NPE"
|
|
Jul 16, 2014 at 11:50 | comment | added | Stephen C |
This explanation is not convincing, considering that LinkedList also supports null list entries.
|
|
Sep 6, 2012 at 18:22 | vote | accept | Alfredo Osorio | ||
Sep 3, 2012 at 15:09 | history | edited | gnat | CC BY-SA 3.0 |
added 388 characters in body
|
Sep 3, 2012 at 15:01 | history | answered | gnat | CC BY-SA 3.0 |