Home arrow static arrow Java Programming [Archive] - converting object[] to float[]
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - converting object[] to float[]
This topic has 5 replies on 1 page.

Posts:30
Registered: 7/1/04
converting object[] to float[]  
Aug 5, 2004 1:03 AM



 
Hi,
Anybody tell me how can i convert object[] to float[]. My code is:

java.util.List value = new ArrayList();
Object[] v = value.toArray();
float[] f = ???
I tried to convert first to String[] and to float[] but it did not work.
Thanks,
 

Posts:2,909
Registered: 13.8.2003
Re: converting object[] to float[]  
Aug 5, 2004 1:19 AM (reply 1 of 5)



 
What objects are inside value?
Are you using 1.5?

You'll need to convert them to primitives yourself unless you can find a toPrimitiveArray() somewhere.
 

Posts:319
Registered: 11/8/00
Re: converting object[] to float[]  
Aug 5, 2004 1:25 AM (reply 2 of 5)



 
Here is an example using the 'Tiger' Release, it does what you want but there is probably a much better way to do it.

It uses three new features of the Java language, genetics, autoboxing / autounboxing and the foreach enhancement to the for loop.

public static void main(String[] args)	{		java.util.List<Float> value = new ArrayList<Float>(); 		value.add(10.10f);		value.add(11.10f);		value.add(12.10f);		value.add(13.10f);		value.add(14.10f);		value.add(15.10f); 		Float[] f = new Float[value.size()];		f = value.toArray(f);  		for (float fl : f)		{			System.out.println(fl);		}	}


Here is the 1.4 example
public static void main(String[] args)	{		java.util.List value = new ArrayList(); 		value.add(new Float(10.10f));		value.add(new Float(11.10f));		value.add(new Float(12.10f));		value.add(new Float(13.10f));		value.add(new Float(14.10f));		value.add(new Float(15.10f));  		Object[] v = value.toArray();		float[] f = new float[v.length]; 		for (int i = 0; i < v.length; i++)		{			f[i] = ((Float)v[i]).floatValue();		}  		for (int i = 0; i < f.length; i++)		{			System.out.println(f[i]);		}	}
 

Posts:30
Registered: 7/1/04
Re: converting object[] to float[]  
Aug 5, 2004 1:27 AM (reply 3 of 5)



 
It contains Float objects.
 

Posts:30
Registered: 7/1/04
Re: converting object[] to float[]  
Aug 5, 2004 1:30 AM (reply 4 of 5)



 
Many thanks for quick replies.
 

Posts:10,967
Registered: 4/7/01
Re: converting object[] to float[]  
Aug 5, 2004 1:27 PM (reply 5 of 5)



 
//create and fill an Object arrayObject[] o =    {        (float)1, (float)2     }//create and load a new float array with these Objectsfloat[] f = new float[o.length]; for (n = 0; n < o.length; n++){    f[n] = (float)o[n];}
 
This topic has 5 replies on 1 page.