This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic compile error

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

UnmodifiableWrapper.java:error: incompatible types
List<UnmodifiableWrapper> myList=Collections.unmodifiableCollection(new ArrayList<UnmodifiableWrapper>());
^
required: List<UnmodifiableWrapper>
found: Collection<UnmodifiableWrapper>
1 error
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry , i got my mistake.
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn’t about generics, was it? You should have written unmodifiableList().
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I can see a logic error there, too.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you worked out what I think the logic error is?

No, i couldn't figure out.
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many elements does your List contain?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that looks more like it!
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, remove() isn’t in that loop. It was late when I wrote about remove()
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, i had made my collection read-only.That's i wanted to test.
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens when you put this line before your for‑each loop, in line 31?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic