aspose file tools*
The moose likes Beginning Java and the fly likes Double array to int array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double array to int array?" Watch "Double array to int array?" New topic
Author

Double array to int array?

Jody Monahan
Greenhorn

Joined: Feb 10, 2010
Posts: 20
Hi all,

I was just wondering is there any way to convert a double array to an integer array?

Thanks in advance,
Jody
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Just with a loop:

>


[Jess in Action][AskingGoodQuestions]
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Casting is obviously not an option, so that leaves iterating the source array, casting the element at every iteration and storing it in the destination array of the desired component type.
Unfortunately System.arraycopy() won't help you here and neither will any of the utility methods provided by the java.util.Arrays class, so you'll have to do code the loop-convert-store routine yourself.

Edit: Sloooooooow.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rory Evans
Greenhorn

Joined: Feb 19, 2010
Posts: 18
Ernest Friedman-Hill wrote:Just with a loop:

>


In the above code, shouldn't it read

Otherwise, the loop will start with element 1 of the array rather than element 0.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

No, the ++i will still be executed after all other code in the loop body has executed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Not that it matters too much most of the time, but you should always use ++i, not i++, in loops like this, because i++ implies just a tiny bit more work for the computer. ++i means "add one to the variable, and use the result", while i++ means "make a copy of the variable, increment the variable, and use the copy." In a very tight loop, making that copy can make a measurable difference (except when the vm is smart enough to realize the copy isn't needed, and optimizes it away.)
Jody Monahan
Greenhorn

Joined: Feb 10, 2010
Posts: 20
Cheers everyone, thanks for all of the replies!
 
 
subject: Double array to int array?