2

I've been trying to convert a Double ArrayList to a Double Array and vice-versa.

This is the code I have for converting Double ArrayList to Double Array, but the program keeps on crashing. Am I missing something? or is the logic wrong all together?

ArrayList<Double> numsy = new ArrayList<Double>();
ArrayList<Double> numsx = new ArrayList<Double>();  
for (int j = 0; j < 100; j++) {
    numsy.add((double)generator.nextFloat());
    numsx.add((double)j);
}
double[] arrayX = new double[numsx.size()];     
double[] arrayY = new double[numsy.size()];
//assigns array x and y the values in the list.
for (int k1 = 0; k1 < numsy.size(); i++) {
    double f = numsy.get(k1);
    arrayY[k1] = f;
    f = numsx.get(k1);
    arrayX[k1] = f;
}
FFT doFFT = new FFT(4);
doFFT.fft(arrayX, arrayY);
//adding the FFT numbers back to the Lists
for (int j = 0; j < 100; j++) {
    numsy.add(arrayY[j]);
    numsx.add(arrayX[j]);
}
4
  • Can you please share your generator? Commented Oct 19, 2012 at 3:11
  • @Yogendra this is possibly a Random object. Commented Oct 19, 2012 at 3:13
  • What type of exception or error? Commented Oct 19, 2012 at 3:15
  • @YogendraSingh the FFT class? its taken from here Commented Oct 19, 2012 at 3:16

3 Answers 3

4

You are instantiating your Arrays before adding values in your ArrayLists:

double[] arrayX = new double[numsx.size()];     
double[] arrayY = new double[numsy.size()];   

This will create arrays with length: 0. Then you'll have a null pointer in your loop below.

UPDATE

Then you have to edit your loop:

for (int k1 = 0; k1 < numsy.size(); i++) {

to this

for (int k1 = 0; k1 < numsy.size(); k1++) {
Sign up to request clarification or add additional context in comments.

4 Comments

Updated my answer. Try again.
I am such a fool! Thank you so much. I have even added the remaining piece of code that adds the manipulated numbers back to the list. does that logic seem right? It works just fine, but is it there a way to make it even better?
I don't see any problems with it. :)
okay thanks :) I was just wondering if I could make it more dynamic and dont relay on that 100 value again and again.
0
ArrayList<Double> numsy = new ArrayList<Double>();
ArrayList<Double> numsx = new ArrayList<Double>();

 for (int j = 0; j < 100; j++) {
     numsy.add((double)generator.nextFloat());
     numsx.add((double)j);
 }

//Declare arrayX and arrayY after adding values to numX and numY

 double[] arrayX = new double[numsx.size()];     
 double[] arrayY = new double[numsy.size()];     
    //assigns array x and y the values in the list.
 for (int k1 = 0; k1 < numsy.size(); k1++) {
   double f = numsy.get(k1);
   arrayY[k1] = f;
   f = numsx.get(k1);
   arrayX[k1] = f;
 }

1 Comment

Hi Ram, even then the program is not working, its still crashing the program. saying that the application is not responding.
-1

Why not try to use the built-in method:
double[] arrayX = numsx.toArray(new double[0]);
double[] arrayY = numsY.toArray(new double[0]);

5 Comments

I did not know a method like this existed in java. I've worked with python for most of my life so far. so my experience with java is very little. let me read more about it
it gives me the following error, the method toArray(T[]) in the type ArrayList<Double> is not applicable for the arguments (double[])
Oh, my mistake, use toArray(new Double[0]);
it says cannot convert from Double[] to double[]
@Jonathan well, so you'll have to use an array of Doubles, but this won't make problems when retrieving the actual values. You can say: double value = aDoubleArray[2]; and as well, double d = 2.4; aDoubleArray[2] = d; the autoboxing and unboxing of java are really wonderful.

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.