• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Arraylist copying

 
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If two array lists are declared like this,



if i want to copy from one list to other say,



i got the no suitable method found error.

how to do this ?

thank you.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjeev Charla wrote:
If two array lists are declared like this,



if i want to copy from one list to other say,



i got the no suitable method found error.

how to do this ?



Simply, if you don't want to violate the type safety of the generics, you can't. Those two declarations are wildcards, and it is possible to have one refer to a generic type that is incompatible with the other.

To do what you want (and keep type safety), you can't use wildcards, and the generic types have to be the same (edit: almost correct, the list being added has to have a generic type that IS-A the generic type of the list being added to).

Henry

 
Sanjeev Charla
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, your post helped me in solving my problem.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Simply, if you don't want to violate the type safety of the generics, you can't. Those two declarations are wildcards, and it is possible to have one refer to a generic type that is incompatible with the other.




In thinking about it a bit more, it is possible to use wildcards *and* still maintain type safety -- meaning it is possible to have wildcards where the possible generic types can't be incompatible with each other. For example, with this....



this should compile fine....



Henry
 
Oh. Hi guys! Look at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic