Make copy of an array

前端 未结 11 2368
庸人自扰
庸人自扰 2020-11-21 23:05

I have an array a which is constantly being updated. Let\'s say a = [1,2,3,4,5]. I need to make an exact duplicate copy of a and call

相关标签:
11条回答
  • 2020-11-21 23:29

    I have a feeling that all of these "better ways to copy an array" are not really going to solve your problem.

    You say

    I tried a for loop like [...] but that doesn't seem to be working correctly?

    Looking at that loop, there's no obvious reason for it not to work ... unless:

    • you somehow have the a and b arrays messed up (e.g. a and b refer to the same array), or
    • your application is multi-threaded and different threads are reading and updating the a array simultaneously.

    In either case, alternative ways of doing the copying won't solve the underlying problem.

    The fix for the first scenario is obvious. For the second scenario you will have to figure out some way of synchronizing the threads. Atomic array classes don't help because they have no atomic copy constructors or clone methods, but synchronizing using a primitive mutex will do the trick.

    (There are hints in your question that lead me to think that this is indeed thread related; e.g. your statement that a is constantly changing.)

    0 讨论(0)
  • 2020-11-21 23:29

    I had a similar problem with 2D arrays and ended here. I was copying the main array and changing the inner arrays' values and was surprised when the values changed in both copies. Basically both copies were independent but contained references to the same inner arrays and I had to make an array of copies of the inner arrays to get what I wanted.

    This is sometimes called a deep copy. The same term "deep copy" can also have a completely different and arguably more complex meaning, which can be confusing, especially to someone not figuring out why their copied arrays don't behave as they should. It probably isn't the OP's problem, but I hope it can still be helpful.

    0 讨论(0)
  • 2020-11-21 23:30

    All solution that call length from array, add your code redundant null checkersconsider example:

    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();
        }
    }
    

    I recommend you not inventing the wheel and use utility class where all necessary checks have already performed. Consider ArrayUtils from apache commons. You code become shorter:

    public void someMethod(int[] a) {
        int[] b = ArrayUtils.clone(a);
    }
    

    Apache commons you can find there

    0 讨论(0)
  • 2020-11-21 23:33

    you can use

    int[] a = new int[]{1,2,3,4,5};
    int[] b = a.clone();
    

    as well.

    0 讨论(0)
  • 2020-11-21 23:36

    If you want to make a copy of:

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

    This is the way to go:

    int[] b = Arrays.copyOf(a, a.length);
    

    Arrays.copyOf may be faster than a.clone() on small arrays. Both copy elements equally fast but clone() returns Object so the compiler has to insert an implicit cast to int[]. You can see it in the bytecode, something like this:

    ALOAD 1
    INVOKEVIRTUAL [I.clone ()Ljava/lang/Object;
    CHECKCAST [I
    ASTORE 2
    
    0 讨论(0)
  • 2020-11-21 23:39

    For a null-safe copy of an array, you can also use an optional with the Object.clone() method provided in this answer.

    int[] arrayToCopy = {1, 2, 3};
    int[] copiedArray = Optional.ofNullable(arrayToCopy).map(int[]::clone).orElse(null);
    
    0 讨论(0)
提交回复
热议问题