作者adrianshum (Alien)
看板java
标题Re: [问题] 字元阵列
时间Thu Apr 10 15:13:48 2008
※ 引述《yangmie (杨咩)》之铭言:
: char[] temp=people.Name.toCharArray();
: 若我将一个字串转成字元阵列後
: 想要把其中的一个字元拿出来判断
: 可以写成
: if(temp[0]=='\n').......
: 这样子吗?
: 我如果这样写会跑出
: ArrayIndexOutOfBoundsException : 0
: 但是若要一个一个print(temp[0])却可以
: 是为什麽呢?
这个明显只是你第一句的地方, 原本的 string 是 empty
string. 转成 char[] 当然是 0 长度的 array. 取 temp[0]
当然就 array out of bounds
你一个一个 print 我猜你是
for (int i = 0; i < temp.length; ++i) {
System.out.print(temp[i]]);
}
这种吧. 这样写你根本就没有取用过 temp[0]
alien
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 202.155.236.82