【问题标题】:ArrayIndexOutOfBoundsExcetion when trying to clone an array尝试克隆数组时的 ArrayIndexOutOfBoundsExcetion
【发布时间】:2020-10-25 08:49:09
【问题描述】:

我正在尝试克隆第一个数组。 有时会抛出ArrayIndexOutOfBoundsExpection?为什么会发生这种情况,我该如何解决?

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

【问题讨论】:

    标签: java arrays clone indexoutofboundsexception


    【解决方案1】:

    要实际克隆数组,您应该使用.clone() 方法。 (请注意,这仅适用于一维数组。)使用for each loop,您应该简单地打印ele,而不是尝试将数组的元素用作索引。

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

    【讨论】:

      【解决方案2】:

      在 for 循环中,element 是数组的项,而不是它的索引。使用项目值作为索引有时会超出数组大小。结果基于您用于填充数组的随机值,因此它只能随机重现。

      【讨论】:

        【解决方案3】:

        在以下增强的for 循环中,您正尝试像使用索引一样访问元素:

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

        数组arr 的最后一个元素可以是16,因此对于这个值,arr[element] 将被转换为arr[16],从而导致ArrayIndexOutOfBoundsException。这是因为arr[] 的大小是16,因此arr[] 中的最后一个索引是15。当您尝试访问 clone[] 的元素时也是如此。

        替换

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

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

        修正后的代码如下:

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

        或者,您可以使用索引样式来访问数组中的元素:

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

        注意:你应该使用int[] clone = arr.clone()来克隆arr[]。当您执行int[] clone = arr 时,数组引用clone[] 将继续引用相同的元素,即在使用clone[] 访问该索引上的值时,arr[] 索引上的任何更改都将以相同的方式反映,例如

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

        示例运行:

        [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]
        

        另一方面,

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

        示例运行:

        [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]
        

        【讨论】:

        • 替换为 System.out.println(ele); ArrayIndexOutOfBoundsExpection 仍然会发生,而且我在 ele 中的数字与在 element 中的数字不同。
        猜你喜欢
        • 1970-01-01
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 2011-09-20
        • 2013-10-08
        相关资源
        最近更新 更多