This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Generic compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic compile error" Watch "Generic compile error" New topic
Author

Generic compile error

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


UnmodifiableWrapper.java:error: incompatible types
List<UnmodifiableWrapper> myList=Collections.unmodifiableCollection(new ArrayList<UnmodifiableWrapper>());
^
required: List<UnmodifiableWrapper>
found: Collection<UnmodifiableWrapper>
1 error


My one regret in life is that I am not someone else.
- Woody Allen
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Sorry , i got my mistake.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
It wasn’t about generics, was it? You should have written unmodifiableList().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
I think I can see a logic error there, too.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Campbell Ritchie wrote:It wasn’t about generics, was it? You should have written unmodifiableList().


Yes it was not about generic, there is a return type mismatch,I should have written over there,

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
That is another way to solve the compiler error, but it is syntactically very different. Have you worked out what I think the logic error is?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Campbell Ritchie wrote:Have you worked out what I think the logic error is?

No, i couldn't figure out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
How many elements does your List contain?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Oh sorry, i didn't post my full program.


one two six ten
one two six ten Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1075)
at UnmodifiableWrapper.main(UnmodifiableWrapper.java:34)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Yes, that looks more like it!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
You do realise that remove() never works in a for‑each loop?
Have you tried to add() an object to the myList after creating the unmodifiable Collection?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
No, remove() isn’t in that loop. It was late when I wrote about remove()
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Yea, i had made my collection read-only.That's i wanted to test.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
And what happens when you put this line before your for‑each loop, in line 31?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Campbell Ritchie wrote:And what happens when you put this line before your for‑each loop, in line 31?


Of course, it would be added, because myList is still like normal ArrayList but my backed unmodifiableList is only read-only, And that element should come in iteration of unmodifiableList at first place.
 
 
subject: Generic compile error
 
Similar Threads
String array to arraylist
tiger - doubt in this code ...
readonly ArrayList
Doubt in Generic collection
How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content