0

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:

List<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));
2
  • What error are you getting? Note that you have a List of double arrays, not a list of doubles. Commented Aug 17, 2011 at 3:12
  • 1
    I suggest moving line 3 to the first line and replacing the two new ArrayList.. statements with the statements in lines 6 & 7. Your code should look like this: QuadtraticActivity c = new QuadraticActivity(); List<Double> x = c.xValueArr; List<Double> values = c.yValueArr; cause the ArrayLists that you created with new are just replaced in lines 6 & 7. Commented Aug 17, 2011 at 3:19

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

Use the Double object instead of the double native type.

List<Double> x = new ArrayList<Double>();
List<Double> values = new ArrayList<Double>();

2 Comments

I didn't downvote, but removed my upvote cause I reread the post, and realized @Owen isn't using the ArrayLists he initialized. Instead, he replaces them in lines 6 & 7. @Owen's question isn't too clear though, so AFAIK, you could still be right, though.
Yeah I see that now. I just answered his question about adding doubles to ArrayList and called it good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.