System.arraycopy方法如何工作? [英] How does the System.arraycopy method work?
问题描述
使用该方法时要键入的语句示例为
A sample of the statement to type when using the method is
System.arraycopy(data, i, data, i+1, data.length-i-1);
根据我的书,它的工作方式是系统将所有元素从 i
向上移动一个位置.但是从直觉上讲,我认为该方法会将 i
之后的所有元素向下移动一个位置.因此,没有足够的空间放置复制的元素.
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?
推荐答案
System.arraycopy
的定义是
System.arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
因此,此代码将复制
data[i .. (i + n - i - 1)] = data[i..(n-1)]
到
data[i+1 .. (i+1 + n-i-1)] = data[i+1..n]
(范围包括前部但不包括后部,并且 n = data.length
)
(where the range includes the front but excludes the back, and n = data.length
)
,因此在 data [i]
处留有空白"点.图形化:
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 |-------------------------|
这篇关于System.arraycopy方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!