Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: using ArrayList to hold double, convert from object

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using ArrayList to hold double, convert from object

    ArrayList macd = new ArrayList();
     
              //Populate the arrays with associated data
              while (closeRS.next()){
                macd.add(closeRS.getString("macd"));
              }

    I am using the above code to populate a few ArrayLists while collecting data from MySQL. I want to then assign the last ArrayList value to a double variable. I can get the length of the array but when I try putting the value in a double var it say's it's a object. "An object is a dynamically created instance of a class type or a dynamically created array." Is there a way to put a double into the ArrayList in the first place or should I convert it afterwards?

    int mdpos = macd.size() - 1;
    double lastmacd = macd.get(mdpos - 1); //Line contains error


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: using ArrayList to hold double, convert from object

    Is there a way to put a double into the ArrayList in the first place
    No. ArrayLists can only hold objects. The compiler will create a Double object(autoboxing) and put the double value in it and then put that into the ArrayList. When you do a get() you will get a Double object back.
    You can then extract your double value from that object.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using ArrayList to hold double, convert from object

    Quote Originally Posted by Norm View Post
    No. ArrayLists can only hold objects. The compiler will create a Double object(autoboxing) and put the double value in it and then put that into the ArrayList. When you do a get() you will get a Double object back.
    You can then extract your double value from that object.

    so the compiler does this for you?

    Object lastmacd = macd.get(mdhpos - 1);
              System.out.println(lastmacd);

    can you do maths with the object? I'd try but i'm not connected to my db atm. like

    double ansx = lastmacd * lastmacd;

    or wait, would that be

    Object ansx = lastmacd * lastmacd;
    Last edited by aueddonline; December 19th, 2011 at 05:54 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: using ArrayList to hold double, convert from object

    can you do maths with the object?
    Write a test program and see what happens.

    Your would want to use the Double class, not Object.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using ArrayList to hold double, convert from object

    i'm not really getting anywhere with this and can't find any simular examples using ArrayList and doubles, this code works but it's a bit of a mess, getting a string and then parsing as a double.

    ArrayList macd = new ArrayList();
     
    while (closeRS.next()){
                macd.add(closeRS.getString("macd"));
              }
    double ans = Double.parseDouble(macd.get(i).toString());

    i've tried getDouble instead of getString

    macd.add(closeRS.getDouble("macd"));

    but can't seem to do anything with the object from then on. Has anyone got a code example of using Double and ArrayLists?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: using ArrayList to hold double, convert from object

    can't seem to do anything with the object from then on
    What do you want to do with the object?

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using ArrayList to hold double, convert from object

    Quote Originally Posted by Norm View Post
    What do you want to do with the object?
    The database contains double values, I want to do maths with them.

    So I want to take the objects from the ArrayList and do maths

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: using ArrayList to hold double, convert from object

    The values are inside of the objects. You will have to call a method to get the values.

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using ArrayList to hold double, convert from object

    I'm having trouble with the two parts,

    what to put in the arraylist and how to get it out again.

    is the first bit of my code correct?

    while (closeRS.next()){
                macd.add(closeRS.getString("macd"));
              }


    or should I be using getDouble?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: using ArrayList to hold double, convert from object

    is the first bit of my code correct?
    How can anyone say if code is correct without a description of what the code is supposed to be doing?

    The compiler will tell you if you have coded it correctly. What does it say about your code?

Similar Threads

  1. Replies: 3
    Last Post: June 11th, 2011, 02:40 PM
  2. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  3. Typecasting of double variable to integer
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM