tags:

views:

322

answers:

2
+2  Q: 

Java memmove?

I'm in the process of porting some old C code to Java and, due to my inexperience with Java, I have encountered a problem locating a memmove equivalent. Is there such a method in Java? I've done enough research on it to assume that I'm overlooking something rather obvious. Thanks in advance.

+7  A: 

You can use System.arraycopy, if all you want to do is shuffle things around in an array.

Specifically, that function permits the source and target to be in the same array, and the ranges are allowed to overlap; so in that sense it's like memmove.

Chris Jester-Young
It's doubtful I would have searched the System class for an array manipulation method. Thanks very much!
Gary Chambers
+4  A: 

Actually, the System.arrayCopy() is the old school approach to this, and should only be used if you're a) stuck on Java 5 or earlier, or b) have to copy between pre-existing arrays. If you want to create a copy of an array you can use Arrays.copyOf() or Arrays.copyOfRange()

Jherico
memmove specifically allows you to between overlapping ranges. (say, copy the range a[0-5] to a[1-6]. Do the ones you mention allow that?
jalf
I think when people ask about a memmove equivalent (as opposed to a memcpy equivalent), usually case (b) applies. Nonetheless, good answer.
Chris Jester-Young
@jalf: As Jherico mentioned in the answer, copyOf and copyOfRange both create new arrays, and cannot be used for copying stuff to the same array.
Chris Jester-Young