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]);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jonathan
  • 2,728
  • 10
  • 43
  • 73

3 Answers3

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++) {
Russell Gutierrez
  • 1,372
  • 8
  • 19
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;
 }
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • Hi Ram, even then the program is not working, its still crashing the program. saying that the application is not responding. – Jonathan Oct 19 '12 at 03:00
-1

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

Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • 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 – Jonathan Oct 19 '12 at 03:22
  • it gives me the following error, the method toArray(T[]) in the type ArrayList is not applicable for the arguments (double[]) – Jonathan Oct 19 '12 at 03:27
  • Oh, my mistake, use `toArray(new Double[0]);` – Mordechai Oct 19 '12 at 03:36
  • it says cannot convert from Double[] to double[] – Jonathan Oct 19 '12 at 03:41
  • @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. – Mordechai Oct 19 '12 at 04:31