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
2009-06-21 02:15:57
It's doubtful I would have searched the System class for an array manipulation method. Thanks very much!
Gary Chambers
2009-06-21 02:26:47
+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
2009-06-21 06:52:55
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
2009-06-21 10:01:21
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
2009-06-21 14:18:42
@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
2009-06-21 14:20:20