File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist copying

 
Sanjeev Charla
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, your post helped me in solving my problem.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic