为什么要复制的范围不包括最终索引处的数组元素?
示例:
class ArrayCopyOfDemo {
public static void main(String[] args) {
char[] copyFrom = {'d', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd'};
char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);
System.out.println(new String(copyTo));
}
}
结果:“咖啡因”
(要复制的范围不包括索引9处的数组元素(包含字符a)。
来源:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
非常感谢您的回答!
最佳答案
来自 Arrays.copyOfRange javadoc:
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
关于java - 在 Java 中复制数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43325478/