简体   繁体   English

使用循环将元素添加到arraylist

[英]adding elements to arraylist using a loop

My problem is very simple but i can't find the way to solve it. 我的问题很简单,但我找不到解决方法。 Basically i want to create an Arraylist and add elements to it using a loop(up to as many elements as i want). 基本上,我想创建一个Arraylist并使用循环将元素添加到其中(多达我想要的元素)。 i'm using the netbeans gui, and whenever i press a button "add" i want to add the string variables name and capital to my arraylist and display it in a TextArea. 我正在使用netbeans gui,每当我按下“添加”按钮时,我都想将字符串变量名称和大写字母添加到我的arraylist中,并在TextArea中显示它。 something like: 就像是:

[london, england,america,united states etc..] 

so far the only thing it does is print the two variables name and capital many times like: 到目前为止,它唯一要做的就是多次打印两个变量的名称和大写,例如:

[londonn, england, london, england etc..]

here is the code: 这是代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String name, capital;
    ArrayList<String> input = new ArrayList<>();
    name = jTextField1.getText();
    capital = jTextField2.getText();
    for(int i=0;i < 10;i++) {
        input.add(name);
        input.add(capital);
        jTextArea4.setText(String.valueOf(input));
    }      
}

尝试将jTextArea4.setText(String.valueOf(input))移到for loop之外

If you're wanting your ArrayList to continually grow, then you need to make it a class variable and not a local variable to you jButton1ActionPerformed . 如果希望ArrayList持续增长,则需要使其成为类变量,而不是jButton1ActionPerformed的局部变量。

Also take out the for loop. 同时取出for循环。 When you're adding a new name and capital, to your ArrayList , you only have to do it once. 将新名称和大写ArrayList添加到ArrayList ,只需执行一次。

Something like this: 像这样:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // Make sure input is a class variable and it will continue to grow                                         
    input.add(jTextField1.getText());
    input.add(jTextField2.getText());
    jTextArea4.setText(String.valueOf(input));      
}

Once your ArrayList is a class variable, you're going to want a way to either clear the ArrayList or remove items from it. 一旦ArrayList是一个类变量,您将需要一种清除ArrayList或从中删除项目的方法。

您需要将以下代码保留在循环之外:

jTextArea4.setText(String.valueOf(input));

you have to remove the for loop becuase you are storing the same values more than one time. 您必须删除for循环,因为您要多次存储相同的值。

you can do like this. 你可以这样

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String name, capital;
ArrayList<String> input = new ArrayList<>();
name = jTextField1.getText();
capital = jTextField2.getText();
    input.add(name);
    input.add(capital);
    jTextArea4.setText(String.valueOf(input));  

} }

here are getting the name and capital values from the text fields and stroing in into the arraylist and then display the values of the arraylist in teh textfield4.. 这里是从文本字段中获取名称和大写值,并输入到arraylist中,然后在textfield4中显示arraylist的值。

if you want to add as much elements you can do but when you are setting the jtextField4 you have to get the last element from the input arraylist becuause the arraylist object contains 10 stings. 如果要添加尽可能多的元素,则可以,但是在设置jtextField4时,必须从输入arraylist中获取最后一个元素,因为arraylist对象包含10个字符串。

like this 像这样

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String name, capital;
ArrayList<String> input = new ArrayList<>();
name = jTextField1.getText();
capital = jTextField2.getText();
for(int i=0;i < 10;i++) {
    input.add(name);
    input.add(capital);
    jTextArea4.setText(String.valueOf(input.get(input.size)));
}      

} }

I hope this will help you. 我希望这能帮到您。

Completely remove the loop. 完全删除循环。 And if you want that arraylist to be useful you must make it a class level variable 而且,如果您希望该arraylist有用,则必须使其成为类级别的变量

ArrayList<String> input = new ArrayList<>();

private void jButton1ActionPerformed(ActionEvent evt) {                                         
String name, capital;
name = jTextField1.getText();
capital = jTextField2.getText();
    input.add(name);
    input.add(capital);
    jTextArea4.setText(String.valueOf(input));

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM