1

A sample of the statement to type when using the method is

System.arraycopy(data, i, data, i+1, data.length-i-1);

The way it works according to my book is that the system move all element from i onward one position up. But intuitively I will think that the method will move all the element after i one position down. So there is empty space to put the copied element.

I am confused now as to how to move elements around in the same array. What does the statement really say?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • 7
    Here are the docs which seem to be comprehensive: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int) – Matt Coubrough Dec 17 '14 at 03:37
  • Read the Javadoc like everybody else does. – user207421 Dec 17 '14 at 04:39
  • possible duplicate of [How to concatenate two arrays in Java?](http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java) – Martín Schonaker Dec 17 '14 at 05:05

1 Answers1

2

The definition of System.arraycopy is

System.arraycopy(Object src, int srcPos,
                 Object dest, int destPos,
                 int length)

Therefore, this code copies

data[i .. (i + n - i - 1)] = data[i..(n-1)]

to

data[i+1 .. (i+1 + n-i-1)] = data[i+1..n]

(where the range includes the front but excludes the back, and n = data.length)

and thus leaves a "blank" spot at data[i]. Graphically:

       [0] [1] [2] [3] ... [i] [i+1] [i+2] ... [n-2] [n-1]
   src                     |-----------------------|
   dest                        |-------------------------|
nneonneo
  • 171,345
  • 36
  • 312
  • 383