首页 > 文档 > arrayCopy()复制数组
2017
07-21

arrayCopy()复制数组

Name

arrayCopy()复制数组

   

Examples

String[] north = { “OH”, “IN”, “MI” };

String[] south = { “GA”, “FL”, “NC” };

arrayCopy(north, south);

println(south);

// Prints updated array contents to the console:

// [0] “OH”

// [1] “IN”

// [2] “MI”

arrayCopy()复制数组 - 第1张  | Processing编程艺术

String[] north = { “OH”, “IN”, “MI”};

String[] south = { “GA”, “FL”, “NC”};

arrayCopy(north, 1, south, 0, 2);

println(south);

// Prints updated array contents to the console:

// [0] “IN”

// [1] “MI”

// [2] “NC”

Description

Copies an array (or part of an array) to another array. The src array is copied to the dst array, beginning at the position specified by srcPosition and into the position specified by dstPosition. The number of elements to copy is determined by length. Note that copying values overwrites existing values in the destination array. To append values instead of overwriting them, use concat().

The simplified version with only two arguments — arrayCopy(src, dst) — copies an entire array to another of the same size. It is equivalent to arrayCopy(src, 0, dst, 0, src.length).

Using this function is far more efficient for copying array data than iterating through a for() loop and copying each element individually. This function only copies references, which means that for most purposes it only copies one-dimensional arrays (a single set of brackets). If used with a two (or three or more) dimensional array, it will only copy the references at the first level, because a two dimensional array is simply an “array of arrays”. This does not produce an error, however, because this is often the desired behavior. Internally, this function calls Java’s System.arraycopy() method, so most things that apply there are inherited.

将数组 (或数组的一部分) 复制到另一个数组。src 数组被复制到 dst 数组, srcPosition 指定的位置开始, dstPosition 指定的位置。要复制的元素数由长度决定。请注意, 复制值会覆盖目标数组中的现有值。若要追加值而不是覆盖它们, 请使用 concat ()

 

只有两个参数的简化版本-arrayCopy (srcdst)-将整个数组复制到另一个大小相同的值。它相当于 arrayCopy (src, 0, dst, 0, src. 长度)

 

对于复制数组数据而言, 使用此函数比遍历 for () 循环并逐个复制每个元素更有效。此函数只复制引用, 这意味着在大多数情况下, 它只复制 one-dimensional 数组 (一组括号)。如果与两个 (或三或更多) 维数组一起使用, 它将只复制第一级的引用, 因为二维数组只是一个数组。但是, 这不会产生错误, 因为这通常是所需的行为。在内部, 此函数调用 java 的系统. arraycopy () 方法, 因此在那里应用的大多数事物都是继承的。

Syntax

arrayCopy(src, srcPosition, dst, dstPosition, length)

arrayCopy(src, dst, length)

arrayCopy(src, dst)

Parameters

src

Object: the source array对象: 源数组

srcPosition

int: starting position in the source array

int: 源数组中的起始位置

dst

Object: the destination array of the same data type as the source array

对象: 与源数组相同的数据类型的目标数组

dstPosition

int: starting position in the destination array

int: 目标数组中的起始位置

length

int: number of array elements to be copied

int: 要复制的数组元素数

Returns

void

Related

concat()



最后编辑:
作者:卡萨布兰卡
卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。