473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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. {
  3.  
  4. **How can I turn the scoresList which is returned from the AddScores method. into an Array?**
  5.  
  6. }
  7.  
  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);
  14.  
  15. return scoresList;
  16.   }
Oct 5 '09 #1
9 18522
JosAH
11,448 Expert 8TB
@ITHELP85
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,

Jos
Oct 5 '09 #2
Frinavale
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,

-Frinny
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
Frinavale
9,735 Expert Mod 8TB
Please post your code so that we have some reference to discuss.

-Frinny
(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
  2.  
  3. **How can I turn the scoresList which is returned from the AddScores method. into an Array here(in Main Method)* 
  4.  
  5. double[] scores = new double[AddScores().size()];
  6.  
  7. AddScores().toArray(scoresList); //This line gives me an error
  8.  
  9.  
  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);  
  16.  
  17. return scoresList; 
  18.   } 
  19.  
Oct 6 '09 #6
Frinavale
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......
  9.  
-Frinny
Oct 6 '09 #7
I see..................Thanks!!!
I
Oct 6 '09 #8
JosAH
11,448 Expert 8TB
@Frinavale
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,

Jos
Oct 6 '09 #9
Frinavale
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 :)


-Frinny
Oct 6 '09 #10

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

Similar topics

5
by: Steve M | last post by:
I have subclassed CollectionBase. I have also implemented GetEnumerator(). I have tried to set the DataSource of a DataGrid to an instance of my subclass. However, the items in the grid are not...
5
by: Tee | last post by:
Hi guys, I am confused about the usage of "ref". Please see this code: private void button3_Click(object sender, System.EventArgs e) {
1
by: Esteban Felipe | last post by:
Hi, thanks for reading. I hope to find some help here before I commit suicide because this is driving me crazy. Please excuse me if this looks like a long post, but I hope that a complete...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
2
by: Allen | last post by:
Hey all, I have a question for you all. I'm learning vb.net and need some help. I have two classes one named Customers and one named CustomersDA. I have to go though Customer for everything. I have...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
7
by: heavyone | last post by:
I am trying to write a removeFirst method for an ArrayList of the generic type. The removeFirst method is supposed to do a few things: 1) If the list is empty it throws an error. 2) It stores the...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.