Search code examples
javaarrayscloneindexoutofboundsexception

ArrayIndexOutOfBoundsExcetion when trying to clone an array


I'm trying to clone the first array. It sometimes throws ArrayIndexOutOfBoundsExpection? Why did that happen and how can I fix it?.

  import java.util.Random;
    
    public class CloneArray {
    
    
        public static void main(String args[]) {
            
             Random rand = new Random();
             
             int[] arr = new Random().ints(16, 1, 16 + 1).sorted().toArray();
             int[] clone = arr;   
            
             for (int element: arr) {
                System.out.println(arr[element]);
             }
             
         System.out.println("---Clone the Array----");
             
             //clone the array 
             for (int ele: clone) {
                System.out.println(clone[ele]);
             }
        
        }
    }

Solution

  • In the following enhanced for loop, you are trying to access an element like you do by using index:

    for (int element: arr) {
        System.out.println(arr[element]);
    }
    

    The array, arr can have the last element as 16 and thus for this value, arr[element] will be translated to arr[16] causing ArrayIndexOutOfBoundsException. This is because the size of arr[] is 16 and therefore the last index in arr[] is 15. Similar is the case when you are trying to access elements of clone[].

    Replace

    System.out.println(arr[element]);
    System.out.println(clone[ele]);
    

    with

    System.out.println(element);
    System.out.println(ele);
    

    The corrected code will be as follows:

    import java.util.Random;
    
    public class Main {
        public static void main(String[] args) {
            Random rand = new Random();
    
            int[] arr = new Random().ints(16, 1, 16 + 1).sorted().toArray();
            int[] clone = arr;
    
            for (int element : arr) {
                System.out.println(element);
            }
            System.out.println("---Clone the Array----");
            for (int ele : clone) {
                System.out.println(ele);
            }
        }
    }
    

    Alternatively, you can use index-style to access the elements from the array:

    import java.util.Random;
    
    public class Main {
        public static void main(String[] args) {
            Random rand = new Random();
    
            int[] arr = new Random().ints(16, 1, 16 + 1).sorted().toArray();
            int[] clone = arr;
    
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
            System.out.println("---Clone the Array----");
            for (int i = 0; i < clone.length; i++) {
                System.out.println(clone[i]);
            }
        }
    }
    

    Note: You should use int[] clone = arr.clone() to clone arr[]. When you do int[] clone = arr, the array reference clone[] will keep referencing the same elements i.e. any change on an index of arr[] will be reflected in the same way when accessing the value on that index using clone[] e.g.

    import java.util.Arrays;
    import java.util.Random;
    
    public class Main {
        public static void main(String[] args) {
            Random rand = new Random();
            int[] arr = new Random().ints(16, 1, 16 + 1).sorted().toArray();
            int[] clone = arr;
    
            System.out.println(Arrays.toString(arr));
            System.out.println(Arrays.toString(clone));
    
            arr[4] = 100;
            System.out.println(Arrays.toString(arr));
            System.out.println(Arrays.toString(clone));
        }
    }
    

    A sample run:

    [1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 9, 11, 13, 13, 14, 16]
    [1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 9, 11, 13, 13, 14, 16]
    [1, 2, 3, 4, 100, 4, 6, 6, 7, 8, 9, 11, 13, 13, 14, 16]
    [1, 2, 3, 4, 100, 4, 6, 6, 7, 8, 9, 11, 13, 13, 14, 16]
    

    On the other hand,

    import java.util.Arrays;
    import java.util.Random;
    
    public class Main {
        public static void main(String[] args) {
            Random rand = new Random();
            int[] arr = new Random().ints(16, 1, 16 + 1).sorted().toArray();
            int[] clone = arr.clone();
    
            System.out.println(Arrays.toString(arr));
            System.out.println(Arrays.toString(clone));
    
            arr[4] = 100;
            System.out.println(Arrays.toString(arr));
            System.out.println(Arrays.toString(clone));
        }
    }
    

    A sample run:

    [1, 2, 2, 2, 2, 3, 6, 6, 7, 7, 11, 13, 14, 14, 14, 15]
    [1, 2, 2, 2, 2, 3, 6, 6, 7, 7, 11, 13, 14, 14, 14, 15]
    [1, 2, 2, 2, 100, 3, 6, 6, 7, 7, 11, 13, 14, 14, 14, 15]
    [1, 2, 2, 2, 2, 3, 6, 6, 7, 7, 11, 13, 14, 14, 14, 15]