You can try using System.arraycopy()
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[5];
System.arraycopy( src, 0, dest, 0, src.length );
But, probably better to use array.clone() in most cases:
int[] src = ...
int[] dest = src.clone();
Answer from Bala R on Stack OverflowYou can try using System.arraycopy()
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[5];
System.arraycopy( src, 0, dest, 0, src.length );
But, probably better to use array.clone() in most cases:
int[] src = ...
int[] dest = src.clone();
you can use
int[] a = new int[]{1,2,3,4,5};
int[] b = a.clone();
as well.
Videos
The Arrays.copyOf(arr1, i + incrementLength); in your code is creating unnecessary multiple copies of arr1 inside the loop. Something that you should avoid doing.
Take a look at this Documentation from Oracle. The second parameter is the number of elements from arr1 that you will be copying to arr2.
Instead, you can add the needed elements to a List and obtain a copy of the array from that list. Something like this:
Copy List<String> list = new ArrayList<>();
for (int i = 0; i < arr1.length; i++) {
if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
list.add(arr[i]);
}
}
arr2 = list.toArray(new String[list.size()]);
Update
If you do not wish to use Lists, you can try this:
Copy String[] arr1 = new String[] { "a", "b", "c", "d", "e" };
String[] arr2 = new String[arr1.length];
int j = 0;
int finalLength = 0;
for (int i = 0; i < arr1.length; i++) {
if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
// add only elements that you need
arr2[j++] = arr1[i];
// keep track of how many elements you have added
finalLength++;
}
}
// truncate array to finalLength
String[] arr3 = Arrays.copyOf(arr2, finalLength);
for (String value : arr3) {
System.out.println(value);
}
This generates the following output:
Copy b
d
e
Hope this helps!
If you want to copy individual elements of the source array, you can't use Arrays.copyOf. Had you copied a continuous sub-range of the array, you could have used System.arraycopy, but that's not what you are doing. Arrays.copyOf can only be used to either copy the entire array or a prefix of the array (i.e. all the elements from the first element until some index).
You have to first calculate the required length of the output array, instantiate the output array and then iterate over the elements of the input array and copy the relevant elements one by one to the output array.
Copypublic static void main(String[] args) {
String[] arr1 = new String[] { "a", "b", "c", "d", "e" };
int newLen = 0;
for (int i = 0; i < arr1.length; i++) {
if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
newLen++;
}
}
String[] arr2 = new String[newLen];
int index = 0;
for (int i = 0; i < arr1.length; i++) {
if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
arr2[index++] = arr1[i];
}
}
for (String value : arr2) {
System.out.println(value);
}
}
This has a disadvantage of iterating over the input array twice, since you can't create the output array until you know how many elements it should contain. You can avoid that by adding the selected elements of the input array to an ArrayList<String>, and then convert the ArrayList to an array.
It produces a shallow copy, i.e. a new array that contains "old" references (to the same objects, those are not being copied).
In particular, if you have nested arrays, those will not be copied. You will just get a new array whose "top level" points to the same "second level" arrays as the original did. Any changes inside those nested arrays will be reflected in both copy and original.
This test suggests that the copy is deep:
No, it does not. When you assign a new object to the "original" array, this does not affect the copy. It is, after all, a copy.
This is the same situation as:
String x = "foo";
String y = x;
x = "bar";
assertEquals(y, "foo");
No "deep copy" here.
Form Java Doc
....the two arrays will contain identical values.
So in case of array containing reference, only reference is copied and not the actual object. Which means a shallow copy.