java - 查找字符串的所有子字符串 - StringIndexOutOfBoundsException

标签 java algorithm indexoutofboundsexception substring

我创建了 Word 类。 Word 有一个构造函数,它接受一个字符串参数和一个方法 getSubstrings,它返回一个包含 word 的所有子字符串的字符串,按长度排序。

例如,如果用户提供输入“朗姆酒”,该方法返回一个 将像这样打印的字符串:

r
u
m
ru
um
rum 

我想连接字符串中的子字符串,用换行符 ("\n") 分隔它们。然后返回字符串。

代码:

    public class Word {
    String word;

    public Word(String word) {
        this.word = word;
    }
    /**
     * Gets all the substrings of this Word.
     * @return all substrings of this Word separated by newline
     */

    public String getSubstrings()
    {
        String str = "";
        int i, j;
        for (i = 0; i < word.length(); i++) {
            for (j = 0; j < word.length(); j++) {
                str = word.substring(i, i + j);
                str += "\n";
            }
        }
        return str;
    }

但它抛出异常:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1911)

我卡在了这一点上。也许,根据这个方法签名 public String getSubstrings(),你还有其他建议。
如何解决这个问题?

最佳答案

异常分析:

来自 Java7 Docs of StringIndexOutOfBoundsException

public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException

Thrown by String methods to indicate that an index is either negative or greater than the size of the string.

来自 Java 7 Docs of substring

public String substring(int beginIndex,int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

我猜是这样的:length of the substring is endIndex-beginIndex 进入String index out of range: -1。我已经对多个案例进行了测试,证明我的假设是正确的,但感谢任何其他证据。

对于 -1: "rum".substring(2,1); 会给你 String index out of range: -1

Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive.

StringIndexOutOfBoundsException 的原因:

在给定的代码片段中,substring 试图获取 endIndex 超过字符串总长度的字符串 (i+j将超过字符串的总长度):

str = word.substring(i, i + j);

考虑当单词“rum”时 i=2 和 j=2 的情况

然后 str=word.substring(2, 4); 不可能的

类似于问题中给出的代码片段的解决方案:

这应该可以解决问题:

 public String getSubstrings()
    {
        String str="",substr = "";
        for (int i = 0; i < word.length(); i++) {
            for (int j = 0; i+j <= word.length(); j++) { //added i+j and equal to comparison
               substr = word.substring(j, i + j); //changed word.substring(i, i + j) to word.substring(j, i + j)
               if("".equals(substr))continue; //removing empty substrings
               str += substr; //added concatenation + operation
               str += "\n";
            }
        }
        return str+word;
    }

测试用例:

对于 word="rum",这将给出输出:

r
u
m
ru
um
rum

关于java - 查找字符串的所有子字符串 - StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17453456/

相关文章:

java - 如何将 ArrayList<customObject> 从一个 Intent 传递到另一个 Intent ?

c++ - 为什么这个 "reduction factor"算法在做 "+ div/2"

algorithm - 椭圆中点算法逆时针版本

java - 数组 [i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

java - Java JComponent 捕获鼠标事件,但未捕获按键事件

java - java私有(private)变量约束

java - 如何使用一种方法收集并返回具有一个泛型类型参数的实例?

algorithm - 找到数组中唯一不成对的元素

java - 在 Java 中检查越界

java - 如何在 java 中使用特定规则对给定字符串进行子字符串化