简体   繁体   English

我的PrintList函数的ArrayIndexOutOfBoundsException我不知道为什么

[英]ArrayIndexOutOfBoundsException for my PrintList function and I have no idea why

I am writing a really simple program which automatically extends the array when the user reaches the limit of the current array. 我正在编写一个非常简单的程序,当用户达到当前数组的限制时,该程序会自动扩展数组。

The problem is that I am getting a java.lang.ArrayIndexOutOfBoundsException when I run my PrintList method and I really don't know why. 问题是,当我运行PrintList方法时,我得到了java.lang.ArrayIndexOutOfBoundsException,但我真的不知道为什么。 It's working perfectly if I use a random number, which is bigger than the array (eg 500), but if I use 如果我使用一个比数组大的随机数(例如500),它可以很好地工作,但是如果我使用

    for (int i = 0; i < stringArray.length; i++)

or 要么

    for (int i = 0; i <= stringArray.length; i++)

I get a nasty exception. 我收到一个令人讨厌的例外。 How do I deal with this and why am I getting it in the first place? 我该如何处理?为什么要首先解决呢?

Thanks a lot for your help! 非常感谢你的帮助!

Here's the source code of my program: 这是我程序的源代码:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int index = 0;
    String[] randomString = new String[1];

    while (index <= randomString.length) {

        out.println("Enter your string");
        String input = keyboard.next();
        randomString[index] = input;
        PrintArray(randomString);
        index++;

        if (index >= randomString.length) {
            ExtendArray(randomString);
            continue;
        }
    }
}

public static void ExtendArray(String[] stringArray) {

    String[] secondArray = new String[stringArray.length * 2];
    // Copy first array into second array
    for (int i = 0; i < stringArray.length; i++) {
        stringArray[i] = secondArray[i];
        stringArray = secondArray;
    }
}

public static void PrintArray(String[] stringArray) {
    for (int i = 0; i < stringArray.length; i++) {
        out.println(" " + stringArray[i]);
    }
}

Java does not work in the methods you are trying to employ. Java无法在您尝试使用的方法中工作。 Everything in Java is passed by value, unless it is a data point in an object. Java中的所有内容均按值传递,除非它是对象中的数据点。 What you are trying to employ is a pass by reference, which is not possible in Java. 您尝试使用的是通过引用传递,这在Java中是不可能的。

What you are trying to do is an already existing data structure called a Vector: http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html 您试图做的是一个已经存在的称为Vector的数据结构: http : //docs.oracle.com/javase/7/docs/api/java/util/Vector.html

I would suggest doing this: (not sure if it will work properly, as my current PC doesn't have dev tools): 我建议这样做:(由于我当前的PC没有开发工具,所以不确定是否可以正常工作):

public static String[] ExtendArray(String[] stringArray) {

    String[] secondArray = new String[stringArray.length * 2];
    // Copy first array into second array
    for (int i = 0; i < stringArray.length; i++) {
        secondArray[i] = stringArray[i];
    }

    return secondArray;
}

then calling it like so in main: 然后在main中这样称呼它:

randomString = ExtendArray(randomString);

Relating to vectors, this is how it works in a Vector class: 关于向量,这是它在Vector类中的工作方式:

public void incrementCount(int count){
    int increment = (count * 2);
    Object newElementData [] = new Object[increment];
    for(int i = 0; i < count; i++)
    {
        newElementData[i] = elementData[i];
    }

    elementData = new Object[increment];

    elementData = newElementData;
}

In this case, elementData is the original array, newElementData is a temp array that acts to up the bounds. 在这种情况下,elementData是原始数组,newElementData是一个临时数组,其作用是增大边界。

You cant get error on your PrintArray method, you get the error on the line before! 您无法在您的PrintArray方法上得到错误,而之前却在行上得到了错误!

randomString[index] = input;

Because if you do this 因为如果你这样做

index <= randomString.length

The last iteration is out of bounds, String of length 10 has values on 0-9. 最后一次迭代超出范围,长度为10的字符串的值在0-9上。 You have to change the while cycle to 您必须将while周期更改为

index < randomString.length

Also your ExtendArray method is NOT functional! 另外,您的ExtendArray方法不起作用!

You are supposed to swap out the randomString array for a new array with double length. 您应该将randomString数组换成长度为两倍的新数组。 You create a new array and copy the contents of the old one to the new one, but don't do anything with the new array. 您创建了一个新数组并将旧数组的内容复制到新数组,但是对新数组不执行任何操作。

I suppose you want the ExtendArray method to return the new array, and set the randomString variable to be the new array. 我想您希望ExtendArray方法返回新数组,并将randomString变量设置为新数组。

You need to return your second array from ExtendArray function: 您需要从ExtendArray函数返回第二个数组:

public static String[] ExtendArray(String[] stringArray) {

    String[] secondArray = new String[stringArray.length * 2];
    // Copy first array into second array
    for (int i = 0; i <= stringArray.length; i++) {
        stringArray[i] = secondArray[i];
    }
    return secondArray;
}

and in your main: 并在您的主要:

randomString = ExtendArray(randomString);

also your while condition should be: 您的while条件也应为:

while (index < randomString.length)

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

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