【发布时间】:2016-02-07 02:13:13
【问题描述】:
问题:我正在尝试从文件中获取字符串输入,然后将其存储在数组列表中,然后尝试搜索特定单词并删除它们,结果输出应该转到另一个文本文件...如果有,此代码可以正常工作列表中只有 2 个元素。但是如果我放更多它会显示 java.lang.IndexOutOfBoundsException index:12 size 12 。我在 Java 方面不是那么好,所以请帮助并为任何愚蠢的错误道歉......
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class replace{
public static void main(String[] args) throws Exception{
String filen = "C:/Users/Damini/Desktop/hi.txt";
FileReader file = new FileReader("C:/Users/Damini/Desktop/rules.txt");
BufferedReader b = new BufferedReader(file);
PrintWriter pw = new PrintWriter(filen);
List<String> temps = new ArrayList<String>(10);
String line = b.readLine();
while(line!= null)
{
temps.add(line);
line = b.readLine();
}
int size = temps.size()-1;
for(int i=0;i<=size;i++)
{
if(temps.get(i).equals("hello"))
{
temps.remove(i);
}
}
pw.println(temps);
pw.close();
System.out.println("output:"+ temps);
b.close();
}
}
【问题讨论】:
标签: java