在java中,解决数组下标越界的问题

// 获取文本内容
public static List<Simple> getText(String filePath) {
List<Simple> simples =
new ArrayList<Simple>();
try {
FileReader reader =
new FileReader(filePath);
BufferedReader bfReader =
new BufferedReader(reader);
String text =
null;
while ((text = bfReader.readLine()) != null) {
String[] texts = text.split(
"");
Simple simple =
new Simple();
simple.
Scheduled_title= texts[0];
simple.
Scheduled_execute_time = texts[1];
simple.
runTime = texts[2];
simples.add(simple);
}
}
catch (Exception e) {
e.printStackTrace();
}
return simples;
}
Input Error: java.lang.ArrayIndexOutOfBoundsException:1
at.com.CenDB.FileInsert.getText(FileInsert .java ;41)
at.com.CenDB.main(FileInsert .java ;53)
能不能帮我解决下问题?

如果您使用的是原始类型,如int,长,字符,短,字节,浮点型,双和字符数组字符串数组,数组的一次声明的大小是固定的,不能元素的主题下,唯一可以删除某些底层的元素被改变的重新分配
一个[指数] =新的价值。

如果作为容器类,如列表的数组,ArrayList的是从集合继承。该接口定义了指定的元素集合
的单个实例中删除的布尔删除(对象o)。那么它的所有子接口也有这种方法。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-02
String[] texts = text.split("");
Scheduled_title= texts[0];
Scheduled_execute_time = texts[1];
runTime = texts[2];
这里不能这样写,因为不能保证texts的长度是多少,这样明确写出texts的下标,那么texts数组的长度就必须大于等于3;如果texts数组长度等于2,那么texts[2];这里就会下标超出。
第2个回答  2014-12-02
在取数据前,先判断一些获取对象是否为空,且长度大于 index+1
相似回答