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:
我有一个组合框,存储“计算机,代码:21”,“历史,代码:31”,并且还可以更改项目数。但是当我编写此代码以获取其项目时:
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]
[电脑,代码= 21]
[Computer,Code=21, History,Code:31]
[计算机,代码= 21,历史,代码:31]
4 个解决方案
#1
3
Because you are appending to the list with bIHELessons.add(..)
. Each subsequent call adds on to the already printed string.
因为您要使用bIHELessons.add(..)附加到列表中。每个后续调用都会添加到已打印的字符串中。
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
.
如果您仍想添加到ArrayList并打印ArrayList中的当前项,则使用System.out.println(bIHELessons.get(i));而不是使用你现在的。我也认为您不需要使用toString(),因为您的对象已经在类型字符串中。
Change System.out.println(bIHELessons.toString());
to System.out.println(lessons);
if you only want to print the string you are currently iterating on.
更改System.out.println(bIHELessons.toString());到System.out.println(课程);如果您只想打印当前正在迭代的字符串。
#2
1
From what I can see your code is doing what it should be doing. Are you wanting to know why you are seeing all items repeated with each additional call to the print screen?
从我所看到的,你的代码正在做它应该做的事情。您是否想知道为什么每次额外调用打印屏幕时都会看到所有项目重复?
That is happening because the toString() method of the List is putting all the items in the list into a single string.
之所以发生这种情况是因为List的toString()方法将列表中的所有项放入单个字符串中。
#3
1
I don't think the problem is with JComboBox but rather with your expectations. System.out.println(bIHELessons.toString());
will print out the entire contents of the bIHELessons
ArrayList. Since you're adding a new String to the ArrayList on each iteration, it's logical that your System.out.println(bIHELessons.toString());
would show a progressive accumulation of content.
我认为问题不在于JComboBox,而在于你的期望。的System.out.println(bIHELessons.toString());将打印出bIHELessons ArrayList的全部内容。由于您在每次迭代时向ArrayList添加一个新的String,因此您的System.out.println(bIHELessons.toString());会显示内容的渐进积累。
Your question isn't clear but you may consider moving the System.out.println outside of your loop and determining if that's what you're looking for.
您的问题不明确,但您可以考虑将System.out.println移到循环之外,并确定这是否是您要查找的内容。
#4
0
You are printing out the ToString() representation of your entire list. If you want to print out the object you could just print out the lessons variable instead.
您正在打印整个列表的ToString()表示。如果要打印出对象,可以打印出课程变量。
#1
3
Because you are appending to the list with bIHELessons.add(..)
. Each subsequent call adds on to the already printed string.
因为您要使用bIHELessons.add(..)附加到列表中。每个后续调用都会添加到已打印的字符串中。
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
.
如果您仍想添加到ArrayList并打印ArrayList中的当前项,则使用System.out.println(bIHELessons.get(i));而不是使用你现在的。我也认为您不需要使用toString(),因为您的对象已经在类型字符串中。
Change System.out.println(bIHELessons.toString());
to System.out.println(lessons);
if you only want to print the string you are currently iterating on.
更改System.out.println(bIHELessons.toString());到System.out.println(课程);如果您只想打印当前正在迭代的字符串。
#2
1
From what I can see your code is doing what it should be doing. Are you wanting to know why you are seeing all items repeated with each additional call to the print screen?
从我所看到的,你的代码正在做它应该做的事情。您是否想知道为什么每次额外调用打印屏幕时都会看到所有项目重复?
That is happening because the toString() method of the List is putting all the items in the list into a single string.
之所以发生这种情况是因为List的toString()方法将列表中的所有项放入单个字符串中。
#3
1
I don't think the problem is with JComboBox but rather with your expectations. System.out.println(bIHELessons.toString());
will print out the entire contents of the bIHELessons
ArrayList. Since you're adding a new String to the ArrayList on each iteration, it's logical that your System.out.println(bIHELessons.toString());
would show a progressive accumulation of content.
我认为问题不在于JComboBox,而在于你的期望。的System.out.println(bIHELessons.toString());将打印出bIHELessons ArrayList的全部内容。由于您在每次迭代时向ArrayList添加一个新的String,因此您的System.out.println(bIHELessons.toString());会显示内容的渐进积累。
Your question isn't clear but you may consider moving the System.out.println outside of your loop and determining if that's what you're looking for.
您的问题不明确,但您可以考虑将System.out.println移到循环之外,并确定这是否是您要查找的内容。
#4
0
You are printing out the ToString() representation of your entire list. If you want to print out the object you could just print out the lessons variable instead.
您正在打印整个列表的ToString()表示。如果要打印出对象,可以打印出课程变量。