Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. for(int i=0; i<5; i++){
  2. B[i]=A[i]
  3. }
  4.  
  5. int[] src = new int[]{1,2,3,4,5};
  6. int[] dest = new int[5];
  7.  
  8. System.arraycopy( src, 0, dest, 0, src.length );
  9.  
  10. int[] a = new int[]{1,2,3,4,5};
  11. int[] b = a.clone();
  12.  
  13. int[] a = {1,2,3,4,5};
  14.  
  15. int[] b = Arrays.copyOf(a, a.length);
  16.  
  17. ALOAD 1
  18. INVOKEVIRTUAL [I.clone ()Ljava/lang/Object;
  19. CHECKCAST [I
  20. ASTORE 2
  21.  
  22. int[] a = {1,2,3,4,5};
  23. int[] b = Arrays.copyOf(a, a.length);
  24. int[] c = a.clone();
  25.  
  26. //What if array a comes as local parameter? You need to use null check:
  27.  
  28. public void someMethod(int[] a) {
  29. if (a!=null) {
  30. int[] b = Arrays.copyOf(a, a.length);
  31. int[] c = a.clone();
  32. }
  33. }
  34.  
  35. public void someMethod(int[] a) {
  36. int[] b = ArrayUtils.clone(a);
  37. }
  38.  
  39. public static void main(String[] args) {
  40. int[] a = {1,2,3};
  41. int[] b = Arrays.copyOfRange(a, 0, a.length);
  42. a[0] = 5;
  43. System.out.println(Arrays.toString(a)); // [5,2,3]
  44. System.out.println(Arrays.toString(b)); // [1,2,3]
  45. }
  46.  
  47. int[] a = new int[5]{1,2,3,4,5};
  48. int[] b = Arrays.copyOf(a, a.length);
  49.  
  50. int[] arrayToCopy = {1, 2, 3};
  51. int[] copiedArray = Optional.ofNullable(arrayToCopy).map(int[]::clone).orElse(null);
  52.  
  53. public static <T> T[] copyOf(T[] original, int newLength)
  54.  
  55. 2770
  56. 2771 public static <T,U> T[] More ...copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
  57. 2772 T[] copy = ((Object)newType == (Object)Object[].class)
  58. 2773 ? (T[]) new Object[newLength]
  59. 2774 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  60. 2775 System.arraycopy(original, 0, copy, 0,
  61. 2776 Math.min(original.length, newLength));
  62. 2777 return copy;
  63. 2778 }
  64.  
  65. public static <T> T[] copyOfRange(T[] original, int from, int to)
  66.  
  67. 3035 public static <T,U> T[] More ...copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
  68. 3036 int newLength = to - from;
  69. 3037 if (newLength < 0)
  70. 3038 throw new IllegalArgumentException(from + " > " + to);
  71. 3039 T[] copy = ((Object)newType == (Object)Object[].class)
  72. 3040 ? (T[]) new Object[newLength]
  73. 3041 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  74. 3042 System.arraycopy(original, from, copy, 0,
  75. 3043 Math.min(original.length - from, newLength));
  76. 3044 return copy;
  77. 3045 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement