copying array by value in java

前端 未结 5 1004
轻奢々
轻奢々 2020-12-19 00:42

I tried to make an independent copy of an array but couldnt get one. see i cannot copy it integer by integer using a for loop because of efficiency reasons. Is there any oth

5条回答
  •  梦毁少年i
    2020-12-19 01:15

    Try using clone () method for this purpose. As I remember this is the only case where Josh Bloch in Effective Java recommended to use cloning.

    int[] temp = arr.clone ();
    

    But arrayCopy is much faster. Sample performance test on array of 3,000,000 elements:

    System.arrayCopy time: 8ms
         arr.clone() time: 29ms
     Arrays.copyOf() time: 49ms
     simple for-loop time: 75ms
    

提交回复
热议问题