How do you set one array's values to another array's values in Java?

后端 未结 2 1988
感动是毒
感动是毒 2021-02-03 13:13

Lets say you had two arrays:

    int[] a = {2, 3, 4};
    int[] b = {4, 5, 6};

How would you set array a to array b and keep them different dif

相关标签:
2条回答
  • 2021-02-03 13:30

    For arrays, take a look at:

    • System.arraycopy();
    • Arrays.copyOf() and Arrays.copyOfRange();
    • Object.clone().

    For ArrayList:

    • ArrayList.clear() and ArrayList.addAll();
    • ArrayList.ArrayList(Collection<? extends E> c);
    • Object.clone().

    I think this should give you enough pointers to make progress with your homework.

    0 讨论(0)
  • 2021-02-03 13:35

    You may want to use clone:

    a = b.clone();
    

    or use arraycopy(Object source, int sourcePosition, Object destination, int destinationPosition, int numberOfElements)

    System.arraycopy(b, 0, a, 0, b.length());
    
    0 讨论(0)
提交回复
热议问题