1

Why do I get an error at .ToArray . (Syntax) Seems like im putting the right paramater value.

Thanks for your response.

import java.util.LinkedList;

public class Vector {


private double doubleComposantes[];
private int intNombreDeComposante = 0;


public Vector(String strComposantes) {

    strComposantes = strComposantes.trim();
    intNombreDeComposante = getNumberOfComposantes(strComposantes);
}

private int getNumberOfComposantes(final String strComposantes) {

    return strComposantes.split(",").length;
}


private double[] getAllComposantes(final String strComposantes) {

    final String[] strComposantesSplitted = strComposantes.split(",");
    LinkedList<Double> doubleComposantesConvertis = new LinkedList<Double>();

    for (String strComposante : strComposantesSplitted) {

        doubleComposantesConvertis.add(Double.valueOf(strComposante));

    }

    int intLongueur = doubleComposantesConvertis.size();
    return doubleComposantesConvertis.toArray(new double[intLongueur]); // error at toArray()
}

}
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
MalikDz
  • 11
  • 1

5 Answers5

5

toArray signature in LinkedList is

public <T> T[] toArray(T[] a)

and since T will be erased to Object at runtime you can use only Object types here, not primitive types. Try with Double instead of double

return doubleComposantesConvertis.toArray(new Double[intLongueur]); 

Notice you would also have to change return type of your method to Double[]


Update:

if you want to return double[] and can use external libraries then you can use Doubles.toArray from guava.

return Doubles.toArray(doubleComposantesConvertis);//will return double[]
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Ah yes! I dont think Double will work. Do you know another alternative or I should write my own method to copy every list elements in a array – MalikDz Nov 18 '13 at 20:26
  • @MalikDz unfortunately you will not be able to use build in methods to create `double[]`. You would have to copy its elements manually or use some external libraries. I suspect guava may have some classes that could be helpful here but I didn't used them yet. – Pshemo Nov 18 '13 at 20:29
  • @MalikDz check my updated answer if you are interested in guava solution. – Pshemo Nov 18 '13 at 20:47
1

Use this instead:

doubleComposantesConvertis.toArray(new Double[intLongueur]);

Notice that the Double is upper case. That's because it's an object instead of a primitive.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • Return type is double[] not Double[] – m0skit0 Nov 18 '13 at 20:28
  • @m0skit0 the return type of the method you're in doesn't change the parameter types of the libraries you use. This could happen with generics, but notice that the `T` of `toArray` has no connection to the `double[]` of his `getAllComposantes`. – Daniel Kaplan Nov 18 '13 at 20:31
  • I mean `return doubleComposantesConvertis.toArray(new Double[intLongueur])` will raise a compile error. – m0skit0 Nov 18 '13 at 20:33
  • @m0skit0 yep. The easiest solution is to change his method signature: http://stackoverflow.com/questions/564392/converting-an-array-of-objects-to-an-array-of-their-primitive-types Or, you can use the apache commons. – Daniel Kaplan Nov 18 '13 at 20:35
0

toArray() receives an array of objects, you're passing an array of double, and it's a primitive not object. Autoboxing doesn't apply for arrays.

user3001267
  • 304
  • 1
  • 4
0

double[] is not Double[]. You either change your return value to Double[] or build manually a double[].

I suggest you avoid using arrays in your code and use a List instead. This way you won't have this problem. Only use arrays when strictly necessary.

private List<Double> getAllComposantes(final String strComposantes) {
    final String[] strComposantesSplitted = strComposantes.split(",");
    final List<Double> doubleComposantesConvertis = new LinkedList<Double>();
    for (String strComposante : strComposantesSplitted) {
        doubleComposantesConvertis.add(Double.valueOf(strComposante));
    }
    return doubleComposantesConvertis;
}

Also make sure you need LinkedList for some reason instead of ArrayList (which will perform better in most cases).

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

The changes mentioned will fix your problem, but really you'd be better off using the 0-argument toArray method.

Floegipoky
  • 3,087
  • 1
  • 31
  • 47