Jack
Jack

Reputation: 103

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

Upvotes: 2

Views: 98

Answers (3)

Unmitigated
Unmitigated

Reputation: 89374

To actually clone the array, you should use the .clone() method. (Note that this will only work for one dimensional arrays.) With a for each loop, you should simply print ele instead of trying to use the elements of the array as indexes.

int[] arr = new Random().ints(16, 1, 16 + 1).sorted().toArray();
int[] clone = arr.clone();  
//...
for (int ele: clone) {
   System.out.println(ele);
}

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79395

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]

Upvotes: 1

Matus
Matus

Reputation: 73

In the for loop element is the item of the array and not its index. Using items value as index will sometimes exceed the array size. The outcome is based on the random values you use to fill the array and therefor it is reproducible only randomly.

Upvotes: 2

Related Questions