简体   繁体   English

数组索引超出范围错误

[英]Array index out of bounds Error

I've been having some trouble with this code I made up, in that I've been getting an ArrayIndexOutofBounds exception error and I have no idea what is going on. 我编写的这段代码遇到了一些麻烦,因为遇到了ArrayIndexOutofBounds异常错误,我不知道发生了什么。 What I want my code to produce is a char array of numbers. 我希望我的代码产生的是一个char型数字数组。

Here's the code: 这是代码:

public class testing {

    static int k = 2;

    public static void main(String args[]) {

        int[] numArr = new int[] { 15, 18, 21 };

        createChar(numArr, k);
    }

    public static char[] createChar(int numArr[], int k) {
        char[] store = new char[k - 1];

        for (int i = 0; i < k; i++) {
            int divide = numArr[i] / 4;

            int numStore = divide % 4;
            charN(numStore, store, k);
        }
        return store;
    }

    public static char[] charN(int numStore, char store[], int k) {
        for (int i = 0; i < k; i++) {
            if (k == 0) {
            } else {
                store[k - 1] = (char) numStore;
                k = k - 1;
            }
        }
        System.out.print(store);
        return store;
    }
}

Thanks a lot! 非常感谢!

As the error suggests, it is due to access outside the bounds of the array. 正如错误所暗示的,这是由于访问超出了数组的边界。 ie. 即。 some bad index access on an array. 数组上的一些错误的索引访问。

char[] store = new char[k - 1];

You are creating a store character array of size k-1 您正在创建大小为k-1store字符数组

and passing k as size to charN() 并将k作为大小传递给charN()

charN(numStore, store, k);

To resolve your problem. 解决您的问题。 change store declaration as below 如下更改商店声明

char[] store = new char[k];

Also, in class Testing , k must be 3 , not 2. where k is the size of the array. 另外,在class Testingk必须为3 ,而不是2。其中k是数组的大小。

static int k = 3;

When size of the array is k, the indices of array run from 0 to k-1 当数组的大小为k时,数组的索引0k-1

For value k = 2 , the below statement: - 对于值k = 2 ,以下语句:-

char[] store = new char[k - 1];

creates an array of size 1 . 创建一个大小为1的数组。 So, you can only access its 0th index . 因此,您只能访问其0th index

But, further in the charN method, you are accessing it's 1st index at: - 但是,进一步在charN方法中,您将在以下位置访问它的1st索引:-

store[k - 1] = (char) numStore;  // Since k = 2, k - 1 = 1.

Change your array creation to: - 将数组创建更改为:-

char[] store = new char[k];

Furthermore, I don't understand why you took k = 2 on first place. 此外,我不明白为什么您将k = 2放在首位。 May be you meant it to be used as index , in which case, your array creation would be of size k + 1 . 可能是您打算将其用作index ,在这种情况下,数组创建的大小将为k + 1 And accordingly, your for loop will iterate till k + 1 , rather than k . 因此,您的for循环将迭代到k + 1 ,而不是k

Also, I don't understand the role of your charN method. 另外,我不了解您的charN方法的作用。 In fact, tt seems strange that first you are iterating over your array in createChar method, and then passing each element to charN method. 实际上,tt似乎很奇怪,首先您要在createChar方法中遍历数组,然后将每个元素传递给charN方法。 And then there also you are iterating over the char array , assigning the same value to multiple index. 然后,您还要遍历char array ,将相同的值分配给多个索引。 Apart from that, you are decrementing k and incrementing i at the same time in the loop. 除此之外,您在循环中同时减少k和增加i That is really strange. 真的很奇怪 And at the end, you are returning your array from both of your methods, but are not using the return value at all. 最后,您从两个方法都返回了array ,但根本没有使用返回值。

It's quite hard to understand what you want to do. 很难理解您想做什么。 But you should consider all the points I stated in the previous paragraph. 但是您应该考虑我在上一段中所说的所有要点。 For each step there, think of why you want to do this? 考虑到那里的每个步骤,为什么要这样做? Is there any alternative, which might be easy? 有没有其他选择可能很容易? Do, I really need to methods with 2 iteration here? 是的,我真的需要在这里进行2次迭代的方法吗?

I suggest you to take a look at your design once again. 我建议您再次看一下您的设计。 The solution which I posted above might solve your compiler error , but there is some problem with your logic . 我上面发布的解决方案可能会解决您的compiler error ,但是您的logic存在一些问题。 You need to take care of that. 您需要注意这一点。

As per your post the statement 根据您的帖子声明

char[] store = new char[k - 1];   

results in a char array of size 1. So in charN method when you try to access the 导致大小为1的char数组。因此,当您尝试访问

store[k - 1] = (char) numStore;

you are trying to access store[1] as 'k' is 2 here. 您正在尝试访问store [1],因为此处的“ k”为2。 Which is wrong. 哪有错 because with store[1] you are trying to access the second element in the array where as the array size is only 1. 因为使用store [1]您试图访问数组中的第二个元素,其中数组大小仅为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM