无错误提醒,但却出现这样的警告
public class Hong
{
public static void main( String args[] )
{
String str = "zhangshuimei";
int len = str.length();
System.out.println( len );
for ( int i = 0; i <= str.length(); i++ )
{
System.out.print( str.charAt(i) );
}
}
}
提示:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.lang.String.charAt(Unknown Source)
at www.Hong.main(Hong.java:161)
12
zhangshuimei
//什么原因啊!~~~~(>_<)~~~~
[解决办法]
i <= str.length() 改成 i < str.length()
数组下标从0开始。