Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for(int i=0; i<5; i++){
- B[i]=A[i]
- }
- int[] src = new int[]{1,2,3,4,5};
- int[] dest = new int[5];
- System.arraycopy( src, 0, dest, 0, src.length );
- int[] a = new int[]{1,2,3,4,5};
- int[] b = a.clone();
- int[] a = {1,2,3,4,5};
- int[] b = Arrays.copyOf(a, a.length);
- ALOAD 1
- INVOKEVIRTUAL [I.clone ()Ljava/lang/Object;
- CHECKCAST [I
- ASTORE 2
- int[] a = {1,2,3,4,5};
- int[] b = Arrays.copyOf(a, a.length);
- int[] c = a.clone();
- //What if array a comes as local parameter? You need to use null check:
- public void someMethod(int[] a) {
- if (a!=null) {
- int[] b = Arrays.copyOf(a, a.length);
- int[] c = a.clone();
- }
- }
- public void someMethod(int[] a) {
- int[] b = ArrayUtils.clone(a);
- }
- public static void main(String[] args) {
- int[] a = {1,2,3};
- int[] b = Arrays.copyOfRange(a, 0, a.length);
- a[0] = 5;
- System.out.println(Arrays.toString(a)); // [5,2,3]
- System.out.println(Arrays.toString(b)); // [1,2,3]
- }
- int[] a = new int[5]{1,2,3,4,5};
- int[] b = Arrays.copyOf(a, a.length);
- int[] arrayToCopy = {1, 2, 3};
- int[] copiedArray = Optional.ofNullable(arrayToCopy).map(int[]::clone).orElse(null);
- public static <T> T[] copyOf(T[] original, int newLength)
- 2770
- 2771 public static <T,U> T[] More ...copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
- 2772 T[] copy = ((Object)newType == (Object)Object[].class)
- 2773 ? (T[]) new Object[newLength]
- 2774 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
- 2775 System.arraycopy(original, 0, copy, 0,
- 2776 Math.min(original.length, newLength));
- 2777 return copy;
- 2778 }
- public static <T> T[] copyOfRange(T[] original, int from, int to)
- 3035 public static <T,U> T[] More ...copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
- 3036 int newLength = to - from;
- 3037 if (newLength < 0)
- 3038 throw new IllegalArgumentException(from + " > " + to);
- 3039 T[] copy = ((Object)newType == (Object)Object[].class)
- 3040 ? (T[]) new Object[newLength]
- 3041 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
- 3042 System.arraycopy(original, from, copy, 0,
- 3043 Math.min(original.length - from, newLength));
- 3044 return copy;
- 3045 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement