英文:
Rewriting array form Object[] to float[] in Java
问题
我需要从一个 TockaXY[] 数组中获取一个 float[] 数组。
以下是一个包含4个元素的 TockaXY[] 示例数组:
[60.039005,86.44917][96.53153,41.086178][19.988914,31.67395][96.84925,41.90731]
但是我需要一个 float[] 数组。我尝试了以下代码:
```java
for (int i = 0; i < objectArray.length; i++)
floatArray[i] = (Float)objectArray[i];
但是我遇到了无法进行强制类型转换的错误。有任何想法吗?
<details>
<summary>英文:</summary>
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?
</details>
# 答案1
**得分**: 1
如果我理解正确,您有一个包含在另一个数组中的数组。
如果您希望保持这种方式,那么您需要在一个数组中创建另一个数组。
因此;
```java
float floatArray[][]; //声明一个二维数组
floatArray = new float[4][2]; //使其长度与您的数组相同
for (int i = 0; i < objectArray.length; i++){
for(int j =0; j<objectArray[i].length; j++){
//将每个值解析为浮点数,并将其赋值给您的新数组
floatArray[i][j] = Float.parseFloat(objectArray[i][j]);
}
}
英文:
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]);
}
}
答案2
得分: 1
首先,您提供的元素实际上不是数组,而是数组的数组。
您可以尝试以下代码将 Object[][]
转换为 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][1]).floatValue();
}
System.out.println(floatArray);
英文:
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);
答案3
得分: 1
假设 TockaXY
类型类似于:
public class TockaXY {
private float x;
private float y;
// 构造函数、获取方法、设置方法等
@Override
public String toString() {
return "[" + x + ", " + y + "]";
}
}
如果你想要一个包含来自 TockaXY[]
每个元素的 x
和 y
值的 float[]
数组,那么 float[]
的大小必须为 TockaXY[]
的大小的 2 倍。
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();
}
英文:
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();
}
答案4
得分: 1
这个:(SomeType) someExpr;
被称为强制转换操作。不幸的是,Java 有 3 个完全不同的事情,它们都看起来完全一样。这真的是一种令人困惑的情况!
强制转换可以将事物转换,也可以在类型中断言泛型,也可以强制转换类型本身。后两者在运行时__什么都不做__(可能会抛出 ClassCastException
异常),它们只是告诉编译器你知道自己在做什么,要把事物视为不同的类型。
唯一能够转换任何东西的是'类型转换' 模式,只有在括号中的类型和你应用它的表达式都是原始类型(可能会触发自动解包,但止于此)时才会生效。
Float
不是原始类型(float
是原始类型),所以在这里你并没有进行类型转换,但你的问题让人感觉你认为自己在进行类型转换。
好的,那么..我该如何修复我的代码?
看起来 TockaXY
是一个类,类似于:
class TockaXY {
public float x, y;
}
从你的问题中不清楚你想要什么。你想要将所有 8 个浮点数放入一个大小为 8 的浮点数组吗?你只想要 'x' 元素吗?只要 'y' 元素吗?
TockaXY 不是一个浮点数(它是一个坐标),所以这不容易,你需要编写代码来实现。例如:
TockaXY[] in = ...;
float[] fs = new float[in.length * 2];
for (int i = 0; i < in.length; i++) {
fs[i * 2] = in[i].x;
fs[(i * 2) + 1] = in[i].y;
}
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论