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. Landon [ May 03, 2004: Message edited by: Landon Blake ]
Hi, 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(); ... System.out.println(ob[i].toString()); I hope that this helped you...
Joined: Dec 04, 2003
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(); Thanks, Landon
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? Rikko
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()]; personList.toArray(people); 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); 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 ]
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.)