4

I have the following fragment of code:

public void doSomething() {
  float array[] = new float[2];
  array[0] = (float) 0.0;
  array[1] = (float) 1.2;
  someMethod(array);
}

public void someMethod(Object value) {
   //need to convert value to List<Float>
}

As you can see above I want to convert the value variable which is an array (but passed as an Object) to List. I tried the following as suggested here: Create ArrayList from array

    new ArrayList<Float> (Arrays.asList(value));

however, it does not compile.

Any hints?

Community
  • 1
  • 1
Jakub
  • 3,049
  • 8
  • 42
  • 63
  • 2
    Whenever a question involves a compile error, it's helpful to include that error message, just like it's helpful to show the stacktrace for runtime errors. – Paul Bellora Sep 28 '11 at 14:54

5 Answers5

8
public void someMethod(Object value) {
    float[] array = (float[]) value;
    List<Float> result = new ArrayList<Float>(array.length);
    for (float f : array) {
        result.add(Float.valueOf(f));
    }
    // ...
}

I don't know why you're not defining the value argument as a float[] rather than an Object, though.

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
4

[Updated per JB Nizet's correction.] If you use Float instead of float for the array, it will work if the compiler knows that value is an array - which it doesn't. Add a cast:

new ArrayList<Float> (Arrays.asList((Float[])value));

or just change the parameter type of value to Float[], and leave out the cast.

Ed Staub
  • 14,730
  • 3
  • 55
  • 86
  • Now we have ClassCastException: [F cannot be cast to [Ljava.lang.Float – Matthew Farwell Sep 28 '11 at 15:18
  • @Matthew - note first line - _If you use Float instead of float for the array_. If it wasn't clear, I meant that it should be declared as: `Float[] array = new Float[2];` – Ed Staub Sep 28 '11 at 15:20
  • @Ed but that wasn't the original problem. – Matthew Farwell Sep 28 '11 at 15:21
  • 1
    @Matthew - I see you've put the same comment on other answers. You're making an assumption that the OP can't or doesn't want to change the type. I'm assuming he can and doesn't care which it is. I understand that in general, it's important to stick to the premises of the original question, but I think this is overkill. You might consider asking the OP, if you really think it's likely that he cares. – Ed Staub Sep 28 '11 at 15:26
  • @Ed. I'm just pointing out that it doesn't fulfil the original questions. It's perfectly acceptable for you to point out that he can change the array. – Matthew Farwell Sep 28 '11 at 15:33
  • Matthew, the question asks for hints about how to make the given example code compile. It doesn't state what part of that code that the solution must or must not change in order to make it compile. – Derek Mahar Sep 28 '11 at 15:41
2

If you want to view a float[] as a List<Float> then Guava's Floats.asList method will get the job done.

Kevin Bourrillion
  • 38,833
  • 12
  • 69
  • 84
1

Change float to Float since Arrays.asList(T...a) cannot return a List<float> because a Java primitive type cannot be passed as an argument to a generic type.

public void doSomething() {
    Float array[] = new Float[2];
    array[0] = 0.0f;
    array[1] = 1.2f;
    someMethod(array);
}

public void someMethod(Object value) {
    List<Float> list = new ArrayList<Float> (Arrays.asList((Float[])value));
    System.out.println(list);
}

prints

[0.0, 1.2]
Derek Mahar
  • 25,458
  • 37
  • 115
  • 164
0

You have an array of floats but you are trying to get a List of Floats. This is what you need:

public void doSomething() {
    Float[] array = new Float[2];
    array[0] = new Float(0.0);
    array[1] = new Float(1.2);
    someMethod(array);
}

public void someMethod(Object value) {
    List<Float> list = Arrays.asList((Float[])value);
    System.out.println(list);
}        
daiscog
  • 9,082
  • 5
  • 39
  • 59