为什么此有关combobox的代码不起作用? [英] why this code about combobox doesn't work?
问题描述
我有一个组合框,用于存储计算机,代码:21",历史记录,代码:31",并且可以更改项数.但是当我编写此代码以获取其项时:
I have a combobox which stores "Computer ,Code:21","History ,Code:31" and also the number of items can be changed.but when I write this code for getting its items:
List<String> bIHELessons = new ArrayList<String>();
for (int i=0;i<jComboBox1.getItemCount();i++) {
String lessons = (String) jComboBox1.getItemAt(i);
if (lessons != null&& lessons.trim().length()!=0) {
bIHELessons.add(lessons);
System.out.println(bIHELessons.toString());
}
}
它将在控制台中显示以下句子:
it will show these sentences in the console:
[Computer,Code = 21]
[Computer,Code=21]
[Computer,Code = 21,History,Code:31]
[Computer,Code=21, History,Code:31]
推荐答案
因为您要使用bIHELessons.add(..)
附加到列表.随后的每个调用都会添加到已经打印的字符串上.
Because you are appending to the list with bIHELessons.add(..)
. Each subsequent call adds on to the already printed string.
如果您仍要添加到ArrayList并打印ArrayList中的当前项目,请使用System.out.println(bIHELessons.get(i));
而不是现在使用的内容.我也不认为您需要使用toString()
,因为您的对象已经是string
类型.
If you want to still add to the ArrayList and print the current item that is in the ArrayList, then use System.out.println(bIHELessons.get(i));
rather than using what you are now. I also don't think you need to use toString()
because your objects are already in the type string
.
如果只想打印当前要迭代的字符串,请将System.out.println(bIHELessons.toString());
更改为System.out.println(lessons);
.
Change System.out.println(bIHELessons.toString());
to System.out.println(lessons);
if you only want to print the string you are currently iterating on.
这篇关于为什么此有关combobox的代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!