469,329 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Help with method that returns an ArrayList

I have a method that returns an ArrayList and in the Main method i want to convert that list to an array.

Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args)
  2. {
  4. **How can I turn the scoresList which is returned from the AddScores method. into an Array?**
  6. }
  8.   public static ArrayList AddScores()
  9.   {
  10. ArrayList<Double> scoresList = new ArrayList<Double>();
  11. scoresList.add(32.3);
  12. scoresList.add(34.3);
  13. scoresList.add(32.6);
  15. return scoresList;
  16.   }
Oct 5 '09 #1
9 17956
11,448 Expert 8TB
I bet you didn't read the API documentation for the List interface and its implementation the ArrayList class, but then I wonder how you managed to work with such a thing in the first place. Read that API documentation, the answer is in there.

kind regards,

Oct 5 '09 #2
9,735 Expert Mod 8TB
Just in case you're wondering...

All the information you need on the ArrayList and any other Java class can be found in the Java API Documentation. This resource should get you pointed in the right direction. I recommend bookmarking the Java API Documentation and using it as your primary resource when developing in Java.

Happy coding,

Oct 6 '09 #3
I know how to use the toArray method from the Arraylist, but I don't understand how to do it in the main method from the returned variable scoresList in the AddScores() method.
Oct 6 '09 #4
9,735 Expert Mod 8TB
Please post your code so that we have some reference to discuss.

(PS. Please post code in [code] tags.)
Oct 6 '09 #5
Expand|Select|Wrap|Line Numbers
  1.  public static void main (String[] args) //main method
  3. **How can I turn the scoresList which is returned from the AddScores method. into an Array here(in Main Method)* 
  5. double[] scores = new double[AddScores().size()];
  7. AddScores().toArray(scoresList); //This line gives me an error
  10.   public static ArrayList AddScores() 
  11.   { 
  12. ArrayList<Double> scoresList = new ArrayList<Double>(); 
  13. scoresList.add(32.3); 
  14. scoresList.add(34.3); 
  15. scoresList.add(32.6);  
  17. return scoresList; 
  18.   } 
Oct 6 '09 #6
9,735 Expert Mod 8TB
Your AddScores() method returns an ArrayList of that contains a bunch of Doubles.

If you call ArrayList's .toArray() method it will return you an Array of doubles.

On line 8 (the one giving you errors) you are not using the toArray method correctly.

The toArray method does not take any parameters and yet you're passing it something.

Why are you doing this?

And what are you doing on line 5?

You should:
  • retrieve the ArrayList by calling the AddScores() method.
  • declare an array of doubles
  • retrieve the array of doubles by calling the ArrayList's .toArray() method.

For example (may not work):
Expand|Select|Wrap|Line Numbers
  1. double[] theScores = AddScores().toArray();
  2. //The AddScores method returns an ArrayList.
  3. //In the above code I am calling the toArray() method
  4. //on the ArrayList returned by the AddScores() method.
  5. //The toArray() method returns an Array of all of the items
  6. //in the ArrayList.
  7. //This means that the toArray method will return an 
  8. //array of doubles......
Oct 6 '09 #7
I see..................Thanks!!!
Oct 6 '09 #8
11,448 Expert 8TB
Unfortunately that doesn't work. The toArray() method returns an array of Objects (Object[]) that can't even be cast explicitly to a double[]. Autoboxing is a meager implementation of generic types. You have to pass an 'exemplar' to the toArray() method and it has to be an array of some type of objects, e.g. Double[]. Here autounboxing won't help you either ...

kind regards,

Oct 6 '09 #9
9,735 Expert Mod 8TB
Thanks Jos,

I figured that the ArrayList was a strongly typed list like the List<Of T> in .NET

I should have looked it up first. I'll know for next time :)

Oct 6 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Allen | last post: by
7 posts views Thread by heavyone | last post: by
2 posts views Thread by hcaptech | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.