0

I am learning to build a surface using JMonkey api. The class Surface has a method

 createNurbsSurface(controlPoints, nurbKnots, uSegments, vSegments, basisUFunctionDegree, basisVFunctionDegree). 

I am trying to make a simple example to understand the meaning of the arguments. However, I can't initialize the second argument:

List<Float>[] nurbKnots

I tried:

List<Float>[] nurbKnots = {new ArrayList<Float>()};

but it complains that you cannot create a generic array of List<Float>.

Could someone show me how to initialize this nurbKnots.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
Esther
  • 372
  • 5
  • 18

2 Answers2

0

It works for non-generic List:

List[] listNonGeneric = new ArrayList[10];

But this won't work:

List<Float>[] listGeneric = new ArrayList<Float>[10];

You have to use:

List<List<Float>> nurbKnots = new ArrayList<>();

and pass the argument as

(List<Float>[])nurbKnots.toArray();
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • I could compile, however I caught the error: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.util.List; at br.unesp.lbbc.testes.TestaJMonkey.(TestaJMonkey.java:51) at br.unesp.lbbc.testes.TestaJMonkey.main(TestaJMonkey.java:69) – Esther Apr 21 '13 at 16:39
0

One friend helped. He told me:

In Java, it's not really possible to have arrays of generic types (safely). You have to allow unchecked assignment. Something like:

@SuppressWarnings("unchecked")
List<Float>[] f = new List[2];
f[0] = new ArrayList<Float>();
f[0].add(0.1);
f[1] = new ArrayList<Float>();
f[1].add(0.2);

But, it worked!

Esther
  • 372
  • 5
  • 18