求大神帮忙!!!
import java.io.BufferedReader;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
class Word{
private String value;
private int count;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Word(String value, int count) {
this.value = value;
this.count = count;
}
}
public class Demo11 {
private static ArrayList<Word> list = new ArrayList<Word>();
private Word token = null;
public static void main(String[] args) {
Demo11 d = new Demo11();
d.readFile("f:\\bb.txt");
d.display(list);
}
private void readFile(String url){
File f= new File(url);
FileReader fr = null;
BufferedReader br = null;
String s = null;
try {
fr = new FileReader(f);
br = new BufferedReader(fr);
while((s=br.readLine())!=null){
getWord(s);
System.out.println(s);
}
//System.out.println("FINAL-SIZE"+list.size());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void getWord(String s){
int count = 1;
String []word = s.split(" ");
System.out.println("0"+" "+word[0]);
Word one = new Word(word[0], count);
list.add(one);
for(int i=1;i<word.length;i++){
System.out.println(i+" "+word[1]);
for(Word w : list){
if(!(word[i].equals(w.getValue()))){
//出现java.util.ConcurrentModificationException错误 token = new Word(word[i], count);
list.add(token);
}else{
count++;
w.setCount(count);
}
}
}
}
private <T extends Word> void display(ArrayList<T> list){
for(Word w : list){
System.out.println("Value"+"="+w.getValue()+" "+"Count"+"="+w.getCount());
}
}
}
--------------------编程问答-------------------- getWord方法改成下面的试试
--------------------编程问答-------------------- 循环又操作,会导致异常。
private void getWord(String s) {
int count = 1;
String[] word = s.split(" ");
System.out.println("0" + " " + word[0]);
Word one = new Word(word[0], count);
list.add(one);
for (int i = 1; i < word.length; i++) {
System.out.println(i + " " + word[1]);
int size = list.size();
for (int j = 0; j < size; j++) {
if (!(word[i].equals(list.get(j).getValue()))) {
//出现java.util.ConcurrentModificationException错误 token = new Word(word[i], count);
list.add(token);
} else {
count++;
list.get(j).setCount(count);
}
}
}
}
for(Word w : list){ --------------------编程问答-------------------- list 在循环过程中是不允许修改的
补充:Java , Java SE