首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.lang.IndexOutOfBoundsException:索引: 3,大小:3

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

Stack Overflow用户
提问于 2014-04-03 04:22:24
回答 6查看 37.9K关注 0票数 1

我不断地得到

代码语言:javascript
复制
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)

我该如何解决这个问题?

代码语言:javascript
复制
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;
    }
}
EN

回答 6

Stack Overflow用户

发布于 2014-04-03 04:27:57

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

票数 2
EN

Stack Overflow用户

发布于 2014-04-03 04:30:39

你的方法似乎有问题。

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2014-04-03 04:28:32

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

代码语言:javascript
复制
for {i=0; i < myArray.length(); i++) {
...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22822204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档