繁体   English   中英

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

[英]java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

我一直在

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)

我该如何解决?

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

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

你的方法似乎有问题。

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

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

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

此行导致您退出索引:

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

你想迭代数组的大小:

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

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

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

你想要的是:

myArray  .size()
list     size of list

更改:

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

至:

for (index = 0; index < myArray.size(); index++) {
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 );

暂无
暂无

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

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