Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 23951
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: 23951
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
 
Quick! Before anybody notices! Cover it up with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic