Help with method that returns an ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ITHELP85
    New Member
    • Nov 2008
    • 8

    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.


    Code:
    public static void main (String[] args)
    {
    
    **How can I turn the scoresList which is returned from the AddScores method. into an Array?**
    
    }
    
      public static ArrayList AddScores()
      {
    ArrayList<Double> scoresList = new ArrayList<Double>();
    scoresList.add(32.3);
    scoresList.add(34.3);
    scoresList.add(32.6);
    
    return scoresList;
      }
    Last edited by Frinavale; Oct 6 '09, 02:07 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by ITHELP85
    I have a method that returns an ArrayList and in the Main method i want to convert that list to an array.
    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

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      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

      Comment

      • ITHELP85
        New Member
        • Nov 2008
        • 8

        #4
        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.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Please post your code so that we have some reference to discuss.

          -Frinny
          (PS. Please post code in [code] tags.)

          Comment

          • ITHELP85
            New Member
            • Nov 2008
            • 8

            #6
            Code:
             public static void main (String[] args) //main method
            { 
              
            **How can I turn the scoresList which is returned from the AddScores method. into an Array here(in Main Method)* 
            
            double[] scores = new double[AddScores().size()];
            
            AddScores().toArray(scoresList); //This line gives me an error
              
            } 
              
              public static ArrayList AddScores() 
              { 
            ArrayList<Double> scoresList = new ArrayList<Double>(); 
            scoresList.add(32.3); 
            scoresList.add(34.3); 
            scoresList.add(32.6);  
              
            return scoresList; 
              }

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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):
              Code:
              double[] theScores = AddScores().toArray();
              //The AddScores method returns an ArrayList.
              //In the above code I am calling the toArray() method
              //on the ArrayList returned by the AddScores() method.
              //The toArray() method returns an Array of all of the items
              //in the ArrayList.
              //This means that the toArray method will return an 
              //array of doubles......
              -Frinny

              Comment

              • ITHELP85
                New Member
                • Nov 2008
                • 8

                #8
                I see............ ......Thanks!!!
                I

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Frinavale
                  For example (may not work):
                  Code:
                  double[] theScores = AddScores().toArray();
                  //The AddScores method returns an ArrayList.
                  //In the above code I am calling the toArray() method
                  //on the ArrayList returned by the AddScores() method.
                  //The toArray() method returns an Array of all of the items
                  //in the ArrayList.
                  //This means that the toArray method will return an 
                  //array of doubles......
                  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

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    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

                    Comment

                    Working...