Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Calling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling "ToArray" From List" Watch "Calling "ToArray" From List" New topic

Calling "ToArray" From List

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
I have a LinkedList named MyList.
I want to transfer the values in MyList to an array named MyArray using the toArray method. I'm not sure how this is coded.
The API for LinkedList states:
public Object[] toArray()

Would my executable statement look like this?:
MyArray [] = MyList.toArray();
Thanks for any suggestions, I'm still learning how to read these API's.
[ May 03, 2004: Message edited by: Landon Blake ]
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
I will show you a method that i use , using the java.util.* package.
My example has an ARRAYLIST, something like your list...
ArrayList a=new ArrayList();
a.add("value 1");
a.add("value 2");
Object ob[]=a.toArray();
I hope that this helped you...
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
That helps some, thanks.....Does that mean I need to specify the type of object held in my array?
For Example:
String MyArray [] = MyList.toarray();
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
Hi Landon,
As you already said in your first method, the toArray() method returns an array of the type Object. If you want your elements from your list (Linkedlist or arraylist at this point are all the same), you have to assign the resulting array to an array of type Object[], or any super type of Object (but thats a little hard in this situation )
So you have to use Object myArray[] = myList.toArray();
Or use an iterator which iterates over the list and cast each element to a String before putting them into a String array
Hope this helps?
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Would my executable statement look like this?:
MyArray [] = MyList.toArray();

No. Two reasons: 1. What the heck is MyArray []? That will never compile. 2. Notice that the toArray method comes in more than one flavor.
Let's say I had a class name Person, and I'd created a List of Person objects named personList. To get a Person[] from this personList, I'd do the following.
Person[] people = new Person[personList.size()];
people = (Person[])personList.toArray(people);
The assignment statement in the second statement isn't necessary. I could have simply used the following, instead.
Person[] people = new Person[personList.size()];
Or, the same thing could be accomplished in a single line, with an extra (empty array) object created.
Person[] people = (Person[])personList.toArray(new Person[0]);
These are all three different variations to accomplish the same thing - creating a Person[] from the personList. I recommend you pick whichever style is clearest for you. Of course, for any of these to work, every component in the personList must be an object of type Person.
For a decent explanation of just what this toArray method is doing, take a look at the documentation.
[ May 03, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
Thanks for all the helpful responses.
Dirk....I can see why your a "sheriff".
Your second example made the most sense to me. Let me see if I am understanding this correctly.
Your first line "Person[] people = new Person[personList.size()];" creates an array of person objects called people and sets the size of the array using the size method of the list interface.
Your second line "personList.toArray(people);" simply calls the toArray method of the list interface with the newly created array from line 1 as an argument.
I appreciate your detailed explanation. I understand how this method works much better now. I also had never seen a method call used as an argument as you did in the first line, although I did know you could use an int variable in that situation. That will open up a lot of possibilities in my code. Thanks again for your help. (I have read and printed copies of the documentation for the collections interface, as well as for List, ArrayList and LinkedList.)
I agree. Here's the link:
subject: Calling "ToArray" From List
It's not a secret anymore!