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开始。