0

I have to get an float[] array from an TockaXY[] array. Example of TockaXY[] with 4 elements:

[60.039005,86.44917][96.53153,41.086178][19.988914,31.67395][96.84925,41.90731]

but I need an float[]. Tried:

for (int i = 0; i < objectArray.length; i++)
            floatArray[i] = (Float)objectArray[i];

But I get error cannot Cast. Any Ideas?

2
  • try Float.valueOf() Commented Aug 16, 2020 at 12:59
  • 4
    Is TockaXY a class that you wrote? If it is, then edit your question and post the code for that class. Also note that Float is a class and float is a primitive and they are not the same thing. Commented Aug 16, 2020 at 12:59

4 Answers 4

1

If i understood it right, you have an array within an array.

if you want to keep it this way than you have to create another array within an array

so;

   float floatArray[][]; //declare a 2D array
   floatArray = new float[4][2]; //make it's length same as yours

    for (int i = 0; i < objectArray.length; i++){
        for(int j =0; j<objectArray[i].length; j++){
           //parse each value as float, and assign it to your new array
           floatArray[i][j] = Float.parseFloat(objectArray[i][j]);
         }
         
    }
        
Sign up to request clarification or add additional context in comments.

1 Comment

They don't, they have an array of instances of TockaXY, and its toString looks like the toString output of array. At least, that is the sensible explanation, given the name 'TockaXY'.
1

First of all your given element is not array, its array of array.

You can try this to convert Object[][] to Float[][].

Object[][] objectArray = { { 60.039005, 86.44917 }, { 96.53153, 41.086178 }, { 19.988914, 31.67 },
                { 96.84925, 41.90731 } };
Float[][] floatArray = new Float[objectArray.length][2];

for (int i = 0; i < objectArray.length; i++) {
  floatArray[i][0] = ((Double) objectArray[i][0]).floatValue();
  floatArray[i][1] = ((Double) objectArray[i][0]).floatValue();
}
System.out.println(floatArray);

1 Comment

Given the name 'TockaXY', it is extremely unlikely the input is an array-of-arrays, with TockaXY being a java.lang.Float-esque wrapper around float. You're making a wild assumption.
1

Assuming TockaXY is something like

public sclass TockaXY {

      private float x;
      private float y;

      //Constructors, getters, setters etc.

      @Override
    public String toString() {
        return "["+ x + ", " + y + "]";
    }
}

and you want a float[] containing the values of x and y from each element of a TockaXY[], the size of the float[] must be 2 * size of TockaXY[].

float [] floatArray = new float[objectArray.length * 2];

for (int i = 0, j=0; i < objectArray.length; i++) {
    floatArray[j++] = objectArray[i].getX();
    floatArray[j++] = objectArray[i].getY();
}

Comments

1

This: (SomeType) someExpr; is called a cast operation. Unfortunately, there are 3 completely different things that java can do, that all look exactly like this. A real guns and grandmas situation!

Casts can convert things, or can assert generics in types, or can coerce types itself. The latter two, at runtime, do nothing (maybe throw ClassCastException), it's just ways to tell the compiler you know what you are doing; to treat things as different types.

The ONLY one that converts anything is the 'type conversion' mode, and that one only kicks in if both the type in the parentheses and the expression you're applying it on are primitive (auto-unboxing may kick in, but it ends there).

Float is not primitive (float is primitive), so you're not doing a type conversion here, but your question makes it sound like you think you are.

Okay, and.. how do I fix my code?

It looks like TockaXY is a class that looks something like:

class TockaXY {
    public float x, y;
}

From your question it is unclear what you want here. Do you want all 8 floats in an 8-sized float array? Do you only want the 'x' elements? Only the 'y' elements?

A TockaXY is not a float (it's a coordinate), so this is not easy, you'd have to program that. For example:

TockaXY[] in = ...;
float[] fs = new float[in * 2];
for (int i = 0; i < in.length; i++) {
    fs[i * 2] = in[i].x;
    fs[(i * 2) + 1] = in[i].y;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.