• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection and Generics

 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why can't I use Collection as reference type here??
 
Himai Minh
Ranch Hand
Posts: 1256
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this code compiles.
 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:But this code compiles.

Yeah but what if we try to get it like
System.out.println(list.get(0))
 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shashank dwivedi wrote:
Himai Minh wrote:But this code compiles.

Yeah but what if we try to get it like
System.out.println(list.get(0))


actually it asks to typecast list to ArrayList<Integer> why so?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shashank dwivedi wrote:Yeah but what if we try to get it like
System.out.println(list.get(0))

Does the Collection interface have a get method defined in it? (The documentation is at java.util.Collection if you want to check).
 
Paul Clapham
Sheriff
Posts: 21000
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would help if you posted an actual question. What we have is some code which doesn't support your original question, followed by some other questions about some code which you haven't posted. So, post some code and a question about that code.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashank,

We can try -



Regards,
Dan
 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
shashank dwivedi wrote:Yeah but what if we try to get it like
System.out.println(list.get(0))

Does the Collection interface have a get method defined in it? (The documentation is at java.util.Collection if you want to check).

Shashank Dwivedi wrote:
Thanks Matthew I checked docs and got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic