• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding to a List  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code works the way I expect it to,converts an array to an arrayList and prints it out




but when I try to add to the list I get an exception




Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)


thanks
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I try to add to the list I get an exception

Yes, that is described in the API doc for the List interface's add() method.

I suppose it is up to the author of the code to decide what methods will be supported.

Others with more info will expand on this: ...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 2 you create a new, empty ArrayList.

But in line 3 you throw that brand new list away, and you make the variable list refer to whatever Arrays.asList(...) returns.

That method does not return a regular ArrayList. Instead, it returns a special implementation of interface List that reflects what is in the array that you created the list from. This special implementation of List does not allow elements to be added to it - that's why you get an UnsupportedOperationException when you try to call the add(...) method on it.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies,

that makes sense Jasper but how come my IDE eclipse in this case automatically lets me use the add method and does not return a compile error?

for example when I type.  add will come up as an option

thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
that makes sense Jasper but how come my IDE eclipse in this case automatically lets me use the add method and does not return a compile error?


Well, it is because it is *not* a compiler error. It is perfectly fine to call the method, and the result of the method call will be a thrown exception. That is how it is specified in the documentation, and of course, valid.

Henry 
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way for the compiler to know whether or not the List implementation supports the add() operation. The exception you're getting is a RuntimeException.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys while on the same subject I could create a new thread but I don't want to many threads active,since it relates to the asList method

I thought when using the copy constructor of lets say an ArrayList in this example it performs a shallow copy is if the original object is altered it will reflect that in the list for example



I expected it to print bye but instead still printed hi even though I edited words[0]?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:I expected it to print bye but instead still printed hi even though I edited words[0]?

You have strange expectations; I would have expected the opposite. Also, new ArrayList<>(Arrays.asList(words)) is double the work for essentially the same result as just Arrays.asList(words).
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I thought when we use the constructor of an ArrayList we are performing a shallow copy,so we are only copying the reference?

so I thought changing words[0] would also change what is in the list
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further tests:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you expect there to be some kind of "entanglement" between the original and the copy? That kind of behavior would be considered a bug in more cases than not.  When you make a copy of something, the expectation is usually that the copy becomes independent of the original.  The kind of entanglement that you are expecting opens you to all kinds of problems.  In general, unless it's documented otherwise, you should assume that once a copy has been made, it will be independent of the original.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They were obviously very careful with the names of methods in the Arrays class; that is why it is called asList() not toList. REading that link above will tell you whether you have now got a copy or not. The same applies to that ArrayList constructor's documentation. You can verify that by looking up the source code
The Source Code wrote:
Now if you are still worried, you can look at Arrays#copyOf() [addition] and Collection#toArray()
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You really don't need the new ArrayList<>() around the Arrays.asList(), as the source for asList() is...

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You really don't need the new ArrayList<>() around the Arrays.asList(), as the source for asList() is...

This implementation returns an instance of java.util.Arrays$ArrayList, not an instance of java.util.ArrayList.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . .
You really don't need the new ArrayList<>() around the Arrays.asList(), as the source for asList() is...

Confusion: try getClass().getName() on the List returned from Arrays.asList. You find it is a private nested class in the Arrays class, which happens to be called ArrayList too. I have been caught out by that one in the past. It shows one needs to beware of implementation details, doesn't it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!