[英]How do I add an integer to an integer ArrayList in a GUI?
I am trying to add the Integer inputted by the user to the ArrayList
. 我试图将用户输入的Integer添加到
ArrayList
。 It's originally a String
, so I converted it to an Integer
. 它最初是一个
String
,所以我将其转换为Integer
。 I added the Integer
to the Arraylist
, but now I'm not sure how to display it. 我将
Integer
添加到Arraylist
,但是现在我不确定如何显示它。 I want to be able to keep adding marks, and have all the marks displayed on the screen. 我希望能够继续添加标记,并在屏幕上显示所有标记。 I tried a for loop, but I'm not sure what the second parameter would be.
我尝试了for循环,但不确定第二个参数是什么。
Edit: for (i=0; ... ; i++)
-- What would go in the second place? 编辑:
for (i=0; ... ; i++)
-第二名会是什么?
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
String strInputMark;
int intInputMark;
strInputMark = txtInputMark.getText();
intInputMark = Integer.parseInt(strInputMark);
ArrayList<Integer> Marks = new ArrayList<>();
int intMarks;
Marks.add(intInputMark);
}
You can try this 你可以试试这个
String strInputMark;
Integer intInputMark;
strInputMark = txtInputMark.getText();
intInputMark = Integer.valueOf(strInputMark);
ArrayList<Integer> Marks = new ArrayList <>();
int intMarks;
Marks.add(intInputMark);
The short answer is i < Marks.size()
简短的答案是
i < Marks.size()
for (int i = 0; i < Marks.size(); i++) {
System.out.println(Marks.get(i));
}
Try this for loop: 试试这个循环:
String output = "";
for(int i = 0; i < Marks.size(); i++){
output += "\n"+Marks.get(i);
}
JOptionPane.showMessageDialog(null, output);
I think the answer is two-fold. 我认为答案有两个。 First, you need to move 'Marks' outside of the method scope so it can be accessible and continually appended two as input continues.
首先,您需要将“标记”移到方法范围之外,以便可以访问它,并在输入继续时连续添加两个。 Second, as @Umesh said, Marks.size() will give you the total number of elements in your arraylist.
其次,正如@Umesh所说,Marks.size()将为您提供arraylist中元素的总数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.