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

Array to List with generics.

Tim Resh
Greenhorn

Joined: Aug 09, 2006
Posts: 19
Hi, is there any simple way to cast an array to a List. I am using a DAO library that returns an Array of objects that I have to loop and recreate into a List everytime I get data


I would like to use something like


I am looking for a simple way to put the array in a list, collection, etc...

The returning array is in keyed order, in other words the list is in the retreived SQL "Order By" sequence. I do not have access to the DAO source code and I do not have time to rewrite or move to another package that would be more flexible.

Thanks
Mark
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Welcome to the Ranch Tim! Dennis's code is exactly right. As a suggestion to you to help you more quickly find answers to your problems, for questions such as this it is probably easier on you to just use Google to find a solution. For example, searching for "Array to ArrayList Java" brings up this exact same code instantly.

We all appreciate questions and they also provide new resources for people to find on Google when searching, but I know from personal experience that it can often times be faster for these smaller questions to just type exactly what you want into Google, even though Dennis was extremely fast in answering!



Everyday in every way, we get a little better.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7640
    
  19

Tim Resh wrote:The returning array is in keyed order, in other words the list is in the retreived SQL "Order By" sequence.

Just as a BTW, if the Users class specifies its own order (or you can specify one for it with a Comparator), you may not even need to worry about whether the array is ordered or not, viz:Of course, it presumes that your ordering can be made unique; otherwise you may end up with fewer items in the Set than you bargained for .
My usual trick is to stick a final comparison for the ID after whatever else you compare on.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
List to Array?

List<Users> userList = new ArrayList<Users>();

Users [] userArray = (Users[]) userList.toArray();
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7640
    
  19

Harsha Smith wrote:List to Array?

Is that a question or a statement? I believe OP wanted to convert the other way.

Winston
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
For converting lists to arrays, we can use the method from the Arrays utility class. However there is no method in Collections class to convert a collection to Array.


I was only trying to show how its done in the reverse way.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7640
    
  19

Harsha Smith wrote:For converting lists to arrays, we can use the method from the Arrays utility class. However there is no method in Collections class to convert a collection to Array.

Actually, there is:and it doesn't need a cast.

Winston
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Didn't I mention "Collections" class?

I was talking about a static method in Collections utility class
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7640
    
  19

Harsha Smith wrote:Didn't I mention "Collections" class?...

You did; my mistake.

However, since Collections generally only contains methods that aren't available elsewhere (even if, arguably, they should have been), it seems logical to me that it wouldn't.

Winston
Tim Resh
Greenhorn

Joined: Aug 09, 2006
Posts: 19
Thanks to all. The Arrays.aslist(users) seems to take more memory and time. I believe a copy is being made in memory. I was looking for a way to "wrap" the array with a list. I believe using the new ArrayList<Users>(users) is the most efficient way.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7640
    
  19

Tim Resh wrote:Thanks to all. The Arrays.aslist(users) seems to take more memory and time. I believe a copy is being made in memory. I was looking for a way to "wrap" the array with a list. I believe using the new ArrayList<Users>(users) is the most efficient way.

Actually, that's precisely what Arrays.asList() runs; and you're quite right: it does copy the array.
You wouldn't want someone mucking about with your array just because you allowed them to run new ArrayList(array) on it would you?

But what you want can be done by wrapping your own class around an array. If you have it extends AbstractList, you only have to implement the get() and size() methods. If you want to be able to update the backing array as well, you'll need to override set(int, E) too.

One point though: If the array is of primitives you'll have to use the relevant wrapper class, and all the boxing and unboxing will likely make it a fair bit slower than an array. But it'll create really fast .

Winston
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Welcome to the Ranch Tim! Dennis's code is exactly right. As a suggestion to you to help you more quickly find answers to your problems, for questions such as this it is probably easier on you to just use Google to find a solution. For example, searching for "Array to ArrayList Java" brings up this exact same code instantly.

We all appreciate questions and they also provide new resources for people to find on Google when searching, but I know from personal experience that it can often times be faster for these smaller questions to just type exactly what you want into Google, even though Dennis was extremely fast in answering!





Yes, Arrays.asList(T[] object) is a bad solution for more than one reason

1. it is slow

2. it produces a list backed by an array.

so if try to add an element to the resulting list, you get unsupported operation exception.


so whats the solution?


solution: List<Users> usersList = new ArrayList<Users>();

Users [] userArray = new Users[5];

Collections.addAll(usersList, userArray);

This should be your preferred solution. it is much faster and can use it is a normal list object
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array to List with generics.