arraycopy

System.arrayCopy() copies object or reference to object?

时间秒杀一切 提交于 2020-01-09 19:55:21
问题 I am having a final class NameAndValue . I copied an array of NameAndValue objects using System.arrayCopy() and when i changed a NameAndValue object in copied array, it gets reflected in the original array. 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

System.arrayCopy() copies object or reference to object?

余生颓废 提交于 2020-01-09 19:54:27
问题 I am having a final class NameAndValue . I copied an array of NameAndValue objects using System.arrayCopy() and when i changed a NameAndValue object in copied array, it gets reflected in the original array. 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

Best way to copy from one array to another [closed]

怎甘沉沦 提交于 2019-12-31 09:44:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . When I run the following code, nothing gets copied - what am I doing wrong? Also, is this the best/most efficient way to copy data from one array to another? 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++) {

Best way to copy from one array to another [closed]

若如初见. 提交于 2019-12-31 09:44:20
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . When I run the following code, nothing gets copied - what am I doing wrong? Also, is this the best/most efficient way to copy data from one array to another? 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++) {

What is the fastest way to set an arbitrary range of elements in a Java array to null?

夙愿已清 提交于 2019-12-22 03:22:57
问题 I know I can simply iterate from start to end and clear those cells but I was wondering if it was possible in any faster way (perhaps using JNI-ed System.arrayCopy )? 回答1: If I got it right, you need to nullify an array, or a sub-range of an array containing references to objects to make them eligible for GC. And you have a regular Java array, which stores data on-heap. Answering your question, System.arrayCopy is the fastest way to null a sub-range of an array. It is worse memory-wise than

Does java.lang.System.arraycopy() use a shallow copy?

末鹿安然 提交于 2019-12-10 11:54:44
问题 System.arraycopy() is a shallow copy method. For an array of a primitive type, it will copy the values from one array to another: int[] a = new int[]{1,2}; int[] b = new int[2]; System.arraycopy(a, 0, b, 0, 2); b will be {1,2} now. Changes in a will not affect b . For an array of a non-primitive type, it will copy the references of the object from one array to the other: MyObject[] a = new MyObject[] { new MyObject(1), new MyObject(2)}; MyObject[] b = new MyObject[2]; System.arraycopy(a, 0, b

What is the fastest way to set an arbitrary range of elements in a Java array to null?

泄露秘密 提交于 2019-12-05 01:11:46
I know I can simply iterate from start to end and clear those cells but I was wondering if it was possible in any faster way (perhaps using JNI-ed System.arrayCopy )? If I got it right, you need to nullify an array, or a sub-range of an array containing references to objects to make them eligible for GC. And you have a regular Java array, which stores data on-heap. Answering your question, System.arrayCopy is the fastest way to null a sub-range of an array. It is worse memory-wise than Arrays.fill though, since you would have to allocate twice as much memory to hold references at worst case

System.arrayCopy is slow

流过昼夜 提交于 2019-11-30 03:58:51
I've been trying to measure the performance of the System.arrayCopy vs Arrays.copyOf in order to choose properly one of them. Just for the sake of benchmark I added manual copy as well and the result surprised me. Obviously I'm missing something really important, could you, please, tell me, what it is? The implementation is as follows (see first 4 methods). 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

Appending a byte[] to the end of another byte[] [duplicate]

删除回忆录丶 提交于 2019-11-30 02:53:55
This question already has an answer here: Easy way to concatenate two byte arrays 12 answers I have two byte[] arrays which are of unknown length and I simply want to append one to the end of the other, i.e.: byte[] ciphertext = blah; byte[] mac = blah; byte[] out = ciphertext + mac; I have tried using arraycopy() but can't seem to get it to work. krock Using System.arraycopy() , something like the following should work: // create a destination array that is the size of the two arrays byte[] destination = new byte[ciphertext.length + mac.length]; // copy ciphertext into start of destination

System.arrayCopy() copies object or reference to object?

旧城冷巷雨未停 提交于 2019-11-29 02:07:52
I am having a final class NameAndValue . I copied an array of NameAndValue objects using System.arrayCopy() and when i changed a NameAndValue object in copied array, it gets reflected in the original array. 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"