Skip to content

Instantly share code, notes, and snippets.

@raviyasas
Created April 25, 2020 20:10
Show Gist options
  • Save raviyasas/e1a690359f41f0b4f4546e14e61ab910 to your computer and use it in GitHub Desktop.
Save raviyasas/e1a690359f41f0b4f4546e14e61ab910 to your computer and use it in GitHub Desktop.
Copy an array
package interviews;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CopyArray {
public static void main(String args[]) {
int[] a1 = {1, 2, 3};
int[] a2 = {4, 5, 6};
int[] a3 = new int[a1.length + a2.length];
System.arraycopy(a1, 0, a3, 0, a1.length);
System.arraycopy(a2, 0, a3, a1.length, a2.length);
System.out.println(Arrays.toString(a3));
String[] s1 = {"aa", "bb"};
String[] s2 = {"cc", "ee"};
List<String> s3 = new ArrayList<>(s1.length + s2.length);
Collections.addAll(s3, s1);
Collections.addAll(s3, s2);
System.out.println(s3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment