Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';当索引1中不存在元素时,以下java代码是否抛出java.lang.StringIndexOutOfBoundsException?_Java_String - Fatal编程技术网

为什么';当索引1中不存在元素时,以下java代码是否抛出java.lang.StringIndexOutOfBoundsException?

为什么';当索引1中不存在元素时,以下java代码是否抛出java.lang.StringIndexOutOfBoundsException?,java,string,Java,String,从Javadoc: String java.lang.String.substring(int beginIndex):返回作为此字符串的子字符串的新字符串。子字符串以指定索引处的字符开始,并延伸到此字符串的结尾 请看下面的例子,并特别注意“空虚”的例子: 如果beginIndex为负值或大于此字符串对象的长度,则抛出异常;在您的情况下,beginIndex等于字符串的长度,而不是更大。请查看,并特别注意“空”示例: 如果beginIndex为负值或大于此字符串对象的长度,则抛出异常;在您的情况

从Javadoc:

String java.lang.String.substring(int beginIndex)
:返回作为此字符串的子字符串的新字符串。子字符串以指定索引处的字符开始,并延伸到此字符串的结尾

请看下面的例子,并特别注意“空虚”的例子:

如果beginIndex为负值或大于此字符串对象的长度,则抛出异常
;在您的情况下,beginIndex等于字符串的长度,而不是更大。

请查看,并特别注意“空”示例:


如果beginIndex为负值或大于此字符串对象的长度,则抛出异常
;在您的情况下,beginIndex等于字符串的长度,而不是更大。

因为它是完全定义的,所以它是0元素后数组的后缀,0元素是空字符串。@Turing85为什么这段代码没有引发任何异常?指定字符串中只有一个元素位于索引0处,我正在尝试获取索引1处的元素。因为它是完全定义的,所以它是0元素后数组的后缀,0元素是空字符串。@Turing85为什么这段代码没有引发任何异常?指定字符串中唯一存在的元素位于索引0处,我正在尝试获取索引1处的元素。
String str="x";
System.out.println(str.substring(1));
Returns a string that is a substring of this string. The substring begins
with the character at the specified index and extends to the end of
this string. 

Examples: 

 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)

Parameters:
beginIndex the beginning index, inclusive.
Returns:
the specified substring.
Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than
                            the length of this String object