Quick Java Question

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
You can't cast the arrays themselves. Yout need to create another array and cast the individual elements.

You would need something like:
 

CrackaLackaZe

Senior member
Jun 29, 2002
922
0
76
damnit...part of the assignment was to write a code with a method set to recieve a float array from a double array from main. And I can't use for loops...it has to all be done recursively. I have all of it written but can't get around this problem.

I guess I could do what you suggested recursively, but are there any more elegant ways around this?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Here is a recursive conversion.

The for loop is much more efficient, but this meets the criteria you gave.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: CrackaLackaZe
damnit...part of the assignment was to write a code with a method set to recieve a float array from a double array from main. And I can't use for loops...it has to all be done recursively. I have all of it written but can't get around this problem.

I guess I could do what you suggested recursively, but are there any more elegant ways around this?

How about a while loop? :)

<-smartass
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: amdfanboy
Originally posted by: CrackaLackaZe
damnit...part of the assignment was to write a code with a method set to recieve a float array from a double array from main. And I can't use for loops...it has to all be done recursively. I have all of it written but can't get around this problem.

I guess I could do what you suggested recursively, but are there any more elegant ways around this?

How about a while loop? :)

<-smartass

He wanted to convert a double[] to a float[], not a float[] to a double[] ;)

<-- smarterass
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: beyonddc
To get this right, first, you shouldn't use casting.

Take a look at the Java API for - double
http://java.sun.com/j2se/1.4.2...uble.html#floatValue()
You should be using Double.floatValue() to let a Double variable to return you back with a float object.

Sure, if he was dealing with Doubles, but he's not; he's dealing with doubles. Besides which, the implementation of floatValue() casts a double to a float anyways so it wouldn't make any difference.

Edit: p.s. Kilsrat, good to see you answering the noob questions ;)
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: Kilrsat
Originally posted by: amdfanboy
Originally posted by: CrackaLackaZe
damnit...part of the assignment was to write a code with a method set to recieve a float array from a double array from main. And I can't use for loops...it has to all be done recursively. I have all of it written but can't get around this problem.

I guess I could do what you suggested recursively, but are there any more elegant ways around this?

How about a while loop? :)

<-smartass

He wanted to convert a double[] to a float[], not a float[] to a double[] ;)

<-- smarterass

Same thing, right? :confused: