using ArrayList to hold double, convert from object
Code :
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?
Code :
int mdpos = macd.size() - 1;
double lastmacd = macd.get(mdpos - 1); //Line contains error
Re: using ArrayList to hold double, convert from object
Quote:
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.
Re: using ArrayList to hold double, convert from object
Quote:
Originally Posted by
Norm
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?
Code :
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
Code :
double ansx = lastmacd * lastmacd;
or wait, would that be
Code :
Object ansx = lastmacd * lastmacd;
Re: using ArrayList to hold double, convert from object
Quote:
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.
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.
Code :
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
Code :
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?
Re: using ArrayList to hold double, convert from object
Quote:
can't seem to do anything with the object from then on
What do you want to do with the object?
Re: using ArrayList to hold double, convert from object
Quote:
Originally Posted by
Norm
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
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.
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?
Code :
while (closeRS.next()){
macd.add(closeRS.getString("macd"));
}
or should I be using getDouble?
Re: using ArrayList to hold double, convert from object
Quote:
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?