5

I think probably this question has been asked/answered several times before my post. But I couldn't get the exact thing I am looking for, so I post it again:

I am trying to do like this:

float[][] fa2 = {{7,2}, {5,4}, {9,6}, {4,7}, {8,1}, {2,3}};
ArrayList<Float[]> AF = new ArrayList<Float[]>();
AF = Arrays.asList(fa2);

But it is giving an error:

Type mismatch: cannot convert from List<float[]> to ArrayList<Float[]>

I understand the reason for the error but what is the most convenient way to do the conversion in Java ?

This is the easiest way that I can think of. Is there something better / more convenient ?

float[][] fa2 = {{7,2}, {5,4}, {9,6}, {4,7}, {8,1}, {2,3}};
ArrayList<Float[]> AF = new ArrayList<Float[]>(fa2.length);
for (float[] fa : fa2) {
    //initialize Float[]
    Float[] Fa = new Float[fa.length];
    //copy element of float[] to Float[]
    int i = 0;
    for (float f : fa) {
        Fa[i++] = Float.valueOf(f);
    }
    //add Float[] element to ArrayList<Float[]>
    AF.add(Fa);
}
2
  • 1
    I would suggest you use a double instead of a float as it has such low precision. Also I wouldn't use Float[] or Double[] if you can avoid it as these are very inefficient. Commented Jan 13, 2013 at 18:05
  • yeah had to convert to double ultimately. Because, it turned out Double is default typecast and had to typecast for each and every element. Thanks! Commented Jan 14, 2013 at 19:35

5 Answers 5

6

As you're converting from float[] to Float[] you have to do it manually:

float[][] fa2 = {{7,2}, {5,4}, {9,6}, {4,7}, {8,1}, {2,3}};
ArrayList<Float[]> AF = new ArrayList<Float[]>();
for(float[] fa: fa2) {
    Float[] temp = new Float[fa.length];
    for (int i = 0; i < temp.length; i++) {
        temp[i] = fa[i];
    }
    AF.add(temp);
}

Or you could just be using float[] all the way:

List<float[]> AF = Arrays.asList(fa2);
Sign up to request clarification or add additional context in comments.

7 Comments

But for the AF.add(fa) part there will be a type mismatch, isn't it?
In this cause autoboxing handles the conversion for you.
Weird! One learns something every day. Which Java version is this from?
It gives error: The method add(Float[]) in the type ArrayList<Float[]> is not applicable for the arguments (float[])
|
2

This ran for me:

float[][] fa2 = {{7f,2f}, {5f,4f}, {9f,6f}, {4f,7f}, {8f,1f}, {2f,3f}};
List<float[]> AF = Arrays.asList(fa2); 

EDIT: If for some reason you MUST mix float[] and Float[] use Apache Commons ArrayUtils.toObject

float[][] fa2 = {{7f,2f}, {5f,4f}, {9f,6f}, {4f,7f}, {8f,1f}, {2f,3f}};
List<Float[]> AF = new ArrayList(fa2.length);
for (float[] fa : fa2) {
  AF.add(ArrayUtils.toObject(fa));
}

2 Comments

I need ArrayList<Float[]>, the problem is converting to Float[] type
Edited with code sample for converting from float[] to Float[] (which really seems to be the gist of the question)
1

Another way you could do this:

    float[][] fa2 = {{7,2}, {5,4}, {9,6}, {4,7}, {8,1}, {2,3}};

   ArrayList<float[]> AF = new ArrayList<float[]>((Collection<float[]>)Arrays.asList(fa2))

I forgot to add the cast.. fixed it.

2 Comments

This is what I get from that one: The constructor ArrayList<Float[]>(List<float[]>) is undefined. Why should it even work?
It gives error: The constructor ArrayList<Float[]>(List<float[]>) is undefined
0

You can just do it like this and you don't need to convert:

Float[][] test = {{2f,3f},{3f,4f}};

The concept that allows to wrap the natives in Java to change into Objects is called AutoBoxing, which is being used here. However, for this to work you either have to specifically say that the numbers are floats or cast them with (float).

4 Comments

And what's the problem about that? He can still use Arrays.asList(...) as now pointed out by others. I just pointed out the part of the code that needed to be changed. A little bit of own work can be expected, can't it?
It's not my downvote, just wanted to point out that your answer is not very helpful.
Yeah no problem. But imho it was sufficient enough for him fixing it on his own.
Just for clarification: sometimes you have no control over the datatypes you get from a library. So you have to work with float[].
0

try

    float[][] fa2 = {{7,2}, {5,4}, {9,6}, {4,7}, {8,1}, {2,3}};
    List<float[]> af = Arrays.asList(fa2);

or

Float[][] fa2 = {{7f,2f}, {5f,4f}, {9f,6f}, {4f,7f}, {8f,1f}, {2f,3f}};
List<Float[]> af = Arrays.asList(fa2);

2 Comments

Note that you do not have to initialize af. That makes for more garbage collection as soon as you set af to a new List on the next line.
Absolutely, I just copypasted the code from the question, will fix my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.