aspose file tools*
The moose likes Beginning Java and the fly likes Arraylist copying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arraylist copying" Watch "Arraylist copying" New topic
Author

Arraylist copying

Sanjeev Charla
Ranch Hand

Joined: Apr 02, 2009
Posts: 90

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sanjeev Charla
Ranch Hand

Joined: Apr 02, 2009
Posts: 90

Thanks Henry, your post helped me in solving my problem.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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
 
subject: Arraylist copying
 
Similar Threads
Problem with cell Editing of three columns
jQuery.get is not working
getblob from db, then convert to byte and write to bitmap/jpeg
decoding URL values
hibernate code and data source