我正在读取文件,然后将一个特定的字符串添加到jcombobox,但我只需要一次。我试过这样的事情:(即使是包含),但它仍然有两次元素
public void beolvas() {
gyarto_cmb.removeAllItems();
try {
BufferedReader be = null;
be = new BufferedReader(new FileReader("F:\\telefonok.txt"));
String sor = null;
while ((sor = be.readLine()) != null) {
StringTokenizer felbont = new StringTokenizer(sor, ";");
String gyarto_meg = felbont.nextToken();
String tel_tip = felbont.nextToken();
double kijel_meret = (double) Double.parseDouble(felbont.nextToken());
String kijel_felbontas = felbont.nextToken();
int tarhely_merete = (int) Integer.parseInt(felbont.nextToken());
int akkumulator_kap = (int) Integer.parseInt(felbont.nextToken());
int telefon_ara = (int) Integer.parseInt(felbont.nextToken());
Gyarto gyart_1 = new Gyarto();
gyart_1.megnevezes = gyarto_meg;
Tipus tipus1 = new Tipus(tel_tip, kijel_meret, kijel_felbontas, tarhely_merete, akkumulator_kap,
telefon_ara);
gyart_1.tipuska.add(tipus1);
telefonok.add(gyart_1);
if (telefonok.indexOf(gyarto_meg) == -1) {
gyarto_cmb.addItem(gyarto_meg);
}
}
} catch (Exception ex) {
System.out.println("Error:" + ex.toString());
}
}
答案 0 :(得分:0)
我没有看到gyarto_cmb的声明,所以我不知道有哪些方法可用
Set<String> added = new HashSet<String>();
while((sor=be.readLine())!=null){
...
if(!added.contains(gyarto_meg)) {
added.add(gyarto_meg);
gyarto_cmb.addItem(gyarto_meg);
}
}