How do you get a float[] out of an ArrayList in Java
I have gotten a float array stuck inside of a ArrayList. the arraylist contains a Boolean and the afore mentioned float array. my courrent code to get it out (which doesn't work) is
(float)((Float)((Number)(DXY.get(0));
I would prefer to not have to change the things inside, 开发者_运维知识库but if absolutely necessary I could pass the Boolean as a 1 in a simple float array.
The ArrayList is being passed from a method in another class which has it's return type declared as ArrayList.
Thanks in advance.
This was posted from a temporary account.
A float array is an Object so you can just cast to that:
float[] floatArray = (float[])DXY.get(0);
the arraylist contains a Boolean and the afore mentioned float array
I think you really really really want to rethink this. You are asking for trouble. Even without generics making heterogeneous collections was a bad idea. A collection should store only like-types (preferably all the same, but parent/child works too).
Essentially if you cannot declare your ArrayList as something like:
List<Foo> list;
list = new ArrayList<Foo>();
then you are making your life far harder than you need to.
What you should really have is something like:
public class Bar
{
private final boolean bool;
private final float[] array;
...
}
and have the method return that instead of the ArrayList.
I once worked at a company that did something like have a number of methods all take Map<String, Object>
where the String was the parameter name and the Object was the value. They did this so they could make the methods more "dynamic" (eg. they didn't want to have to fix things if they changed the # or type of parameters). Thankfully I had left the company already and only header about it from a friend... if not I would have quit on the spot.
精彩评论