我很惊讶在Java源代码中看到System.arraycopy是一个本机方法.
当然原因是因为它更快.但是什么原生技巧是能够使用的代码使其更快?
为什么不循环遍历原始数组并将每个指针复制到新数组 - 当然这不是那么缓慢和麻烦?
我有两个byte[]
长度未知的数组,我只想将一个附加到另一个的末尾,即:
byte[] ciphertext = blah;
byte[] mac = blah;
byte[] out = ciphertext + mac;
Run Code Online (Sandbox Code Playgroud)
我试过使用arraycopy()
但似乎无法让它工作.
只是好奇:
有人知道为什么该方法System.arraycopy
使用的Object
类型src
和dest
?可以完全使用Object[]
吗?
为什么定义:
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Run Code Online (Sandbox Code Playgroud)
代替
arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int length)
Run Code Online (Sandbox Code Playgroud)
?
当我运行以下代码时,没有任何东西被复制 - 我做错了什么?
另外,这是将数据从一个数组复制到另一个数组的最佳/最有效的方法吗?
public class A {
public static void main(String args[]) {
int a[] = { 1, 2, 3, 4, 5, 6 };
int b[] = new int[a.length];
for (int i = 0; i < a.length; i++) {
a[i] = b[i];
}
}
}
Run Code Online (Sandbox Code Playgroud) 我知道我可以简单地从迭代start
到end
和清除这些细胞,但我想知道,如果它(可能使用JNI-ED是任何更快的方式可能System.arrayCopy
)?
我正在上一堂课NameAndValue
.我复制了一个对象数组,当我更改复制数组中的NameAndValue
对象System.arrayCopy()
时NameAndValue
,它会反映在原始数组中.
public final class NameAndValue
{
public String name;
public String value;
public NameAndValue()
{
}
public NameAndValue(String name,String value)
{
this.name = name;
this.value = value;
}
}
public class Main
{
public static void main(String[] args)
{
NameAndValue[] nv = new NameAndValue[4];
nv[0] = new NameAndValue("A", "1");
nv[1] = new NameAndValue("B", "2");
nv[2] = new NameAndValue("C", "3");
nv[3] = new NameAndValue("D", "4");
NameAndValue[] nv2 = new NameAndValue[4];
System.arraycopy(nv, 0, nv2, …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试测量System.arrayCopy与Arrays.copyOf的性能,以便正确选择其中一个.仅仅为了基准测试我也添加了手动副本,结果让我感到惊讶.显然我错过了一些非常重要的东西,请你,告诉我,它是什么?实现如下(参见前4种方法).
public class ArrayCopy {
public static int[] createArray( int size ) {
int[] array = new int[size];
Random r = new Random();
for ( int i = 0; i < size; i++ ) {
array[i] = r.nextInt();
}
return array;
}
public static int[] copyByArraysCopyOf( int[] array, int size ) {
return Arrays.copyOf( array, array.length + size );
}
public static int[] copyByEnlarge( int[] array, int size ) {
return enlarge( array, size );
}
public static int[] copyManually( …
Run Code Online (Sandbox Code Playgroud) 我写了一些我认为在某些情况下应该会失败的代码,但事实并非如此。我正在做一个arraycopy(),在某些情况下,它会要求复制到越界索引,但是在所有这种情况下,传递给arraycopy()的长度将为0。
我唯一的猜测是Java的arraycopy()实现首先检查length = 0,如果返回则不检查索引参数?我找不到任何关于arraycopy()内部工作方式的参考。
如果这是Java实现的方式,并且代码运行良好,那么我的直觉告诉我,我仍然应该编写代码,这样就不会发生。我应该为此担心吗?
代码是:
if (manyItems == data.length) {
ensureCapacity(manyItems * 2 + 1);
}
if (manyItems == currentIndex) {
data[currentIndex] = element;
}
else { // if data.length = 10, manyItems = 9, currentIndex = 8,
// currentIndex + 2 = 10, which is out of bounds.
// But manyItems - currentIndex -1 = 0, so nothing is copied.
System.arraycopy(data, currentIndex + 1, data, currentIndex + 2,
manyItems - currentIndex - 1);
data[currentIndex + 1] …
Run Code Online (Sandbox Code Playgroud) 假设我有这样的代码;
public void insert(Student[] stus)
{
int count = 0;
for(Student s: stus)
{
s.setId( bla bla);
stus[count].setId(bla bla) // is this line needed?
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我从增强的for循环更改s上的任何内容,我是否也可以看到stus数组中的更改?循环复制的增强如何在参数或其他东西中起作用?
Java的定义在arraycopy
这里:
Java arraycopy
我的Java代码如下:
public byte[] getRawData() {
byte[] raw = new byte[value.length + 4];
//little endian defined by fido spec
raw[0] = (byte) (tag);
raw[1] = (byte) (tag>>8);
raw[2] = (byte) (len);
raw[3] = (byte) (len>>8);
System.arraycopy(value, 0, raw, 4, value.length);
return raw;
}
Run Code Online (Sandbox Code Playgroud)
这是我的Swift代码:
public func getRawData() -> [UInt8] {
var raw = [UInt8(value.count + 4)]
raw[0] = UInt8(tag)
raw[1] = UInt8(tag>>8)
raw[2] = UInt8(len)
raw[3] = UInt8(len>>8)
// The place the put swift arraycopy
return …
Run Code Online (Sandbox Code Playgroud) ArrayList<String> al = new ArrayList(Arrays.asList(Answers));
Collections.shuffle(al);
char answer=(char) (al.indexOf(right)+65);
for(int i=0;i<al.size();i++){
al.set(i, ((char)(i+65))+")"+al.get(i));
}
String[] s=al.toArray(new String[al.size()]);
int n = s.length+1;
String[] ret = new String[n];
System.arraycopy(s,0,ret,1,n);
ret[0]=answer+"";
return ret;
Run Code Online (Sandbox Code Playgroud)
我希望它工作而不是崩溃,但我得到了这个:"线程中的异常"主"java.lang.ArrayIndexOutOfBoundsException"在这一行:
System.arraycopy(s,0,ret,1,n);
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,欢迎任何帮助.