adding a double to an ArrayList
I wrote an android app that solves quadratic equations and then is supposed to graph them. The library I am using requires the data sets to be in ArrayList form. However the loop I have to create the array involves adding a double to them which you cannot do with array lists. Here is the code:
开发者_如何学PythonList<double[]> x = new ArrayList<double[]>();
List<double[]> values = new ArrayList<double[]>();
QuadraticActivity c = new QuadraticActivity();
x=(c.xValueArr);
values=(c.yValueArr);
for (; c.xCurrent <= c.xEnd; c.xCurrent += c.xStep) {
double yCurrent = (c.a)*Math.pow(c.xCurrent, 2) + (c.b)*c.xCurrent + (c.c);
c. xValueArr .add ((c.xCurrent));
c. yValueArr .add ((yCurrent));
Judging from your code, c.xValueArr
and c.yValueArr
are of type, double[]
. If so, then just change the first 7 lines of code to this:
QuadraticActivity c = new QuadraticActivity ();
ArrayList<Double> x = Arrays.asList (c.xValueArr);
ArrayList<Double> values = Arrays.asList (c.yValueArr);
Note: You're instantiating two new ArrayLists in lines 1 and 2
but are replacing them in lines 6 and 7
. Also, you'll have to add the new values inside the loop manually, as x
and values
don't point to the same arrays as c
's arrays. A better fix would be to declare and initialize x
and values
after the iteration, but since you didn't include a closing }
, I'm assuming you need them in the iteration.
ArrayList
should work fine with Integer
or Double
because they are as good as other object. Note that they must have the first letter capitalized.
You should also consider Double.valueOf(double d)
which returns an object of Double for your use case
Is it possible to use a subclass of Number
? It wraps a value in an object. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Number.html
Try Double with a capital 'D'.
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html
Use the Double object instead of the double native type.
List<Double> x = new ArrayList<Double>();
List<Double> values = new ArrayList<Double>();
精彩评论