java.lang.IndexOutOfBoundsException:索引:3,大小:3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22822204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:07:27  来源:igfitidea点击:

java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

java

提问by Xyphoris

I keep getting

我不断得到

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at hartman.ShortestString.printShortestString(ShortestString.java:40)
at hartman.ShortestString.main(ShortestString.java:28)

How do i fix this?

我该如何解决?

package hartman;

import java.util.ArrayList;
import java.util.Scanner;

public class ShortestString {

    public static void main(String[] args) {
        System.out.printf("WELCOME TO SHORTEST STRING\n\n");
        System.out.printf("Type \".\" when done entering data.\n\n");

        ArrayList<String> myArray = new ArrayList<>();
        Scanner keyboard = new Scanner(System.in);
        boolean keepAsking = true;

        while (keepAsking) {
            System.out.printf("Enter string: ");
            String userInput = keyboard.nextLine();

            if (userInput.equals(".")) {
                keepAsking = false;
            } else {
                myArray.add(userInput);
            }
        }

        printShortestString(myArray);
        System.out.printf("\n\nGOODBYE!\n");
        keyboard.close();
    }

    public static void printShortestString(ArrayList<String> myArray) {

        int index;
        int index1 = 1;

        for (index = 0; index < myArray.get(index).length(); index++) {
            if (myArray.get(index).length() < myArray.get(index1).length()) {
                System.out.printf("\nShortest string is \"%s\" with length %d",
                        myArray.get(index), myArray.get(index).length());
            } else {
                index1++;
            }
        }
        return;
    }
}

回答by Adam

Try using for (index = 0; index < myArray.length(); index++) {for line 40. You're using the length of the string at element index, not the length of the ArrayList.

尝试使用for (index = 0; index < myArray.length(); index++) {第 40 行。您使用的是 element 处的字符串index长度,而不是 ArrayList 的长度。

回答by NigelO

We'll need to see the source of printShortestString(), but I'll bet you need to change your for loop to break when the index, i, is one less than the length of your array, as opposed to equal to the length of the array:

我们需要查看 的来源printShortestString(),但我敢打赌,当索引 ,i比数组的长度小 1时,您需要更改 for 循环以中断,而不是等于数组的长度:

for {i=0; i < myArray.length(); i++) {
...
}

回答by Pankaj Gadge

This line causing you to go out of index:

这行导致您退出索引:

for (index = 0; index < myArray.get(index).length(); index++)

You want to iterate over the size of array :

您想遍历数组的大小:

for (index = 0; index < myArray.size(); index++)

回答by But I'm Not A Wrapper Class

Your logic behind printShortestString(...)is incorrect. You want to loop through the list size (not the String size). What's happeneing here is:

你背后的逻辑printShortestString(...)不正确。您想遍历列表大小(而不是字符串大小)。这里发生的事情是:

myArray   .get(index)                             .length()
list      object at a given index of this list    size of that object

What you want is:

你想要的是:

myArray  .size()
list     size of list

Change:

改变:

for (index = 0; index < myArray.get(index).length(); index++) {

To:

到:

for (index = 0; index < myArray.size(); index++) {

回答by RoverMAX

Your method seems to have problem.

你的方法好像有问题。

public static void printShortestString(ArrayList<String> myArray) {
    if (myArray.isEmpty()) return;
    int len = myArray.get(0).length();
    int shortestIndex = 0;
    for (int index = 1; index < myArray.size(); index++) {
        if (myArray.get(index).length() < myArray.get(index - 1).length()) {
            len = myArray.get(index).length();
            shortestIndex = index;
        } 

    }
    System.out.printf("\nShortest string is \"%s\" with length %d",
                myArray.get(shortestIndex), len);
    return;
}

回答by Pat B

String word = null;
for (index = 0; index < myArray.size(); index++) {

    if (word == null) {
        word = myArray.get(index);
        continue;
    }

    if (myArray.get(index).length() < word.length()) {
        word = myArray.get(index).length()
    }
}
System.out.println("The shortest word is:" + word );