java 将用户输入添加到 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27673561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:12:33  来源:igfitidea点击:

Adding User Input to ArrayList

javaarraylistuser-input

提问by Démíe Edúardsson

I have an array list, I type in something into it and it saves just the last thing I typed, but I need things that were typed before. How do I fix it?

我有一个数组列表,我在其中输入了一些内容,它只保存了我最后输入的内容,但我需要之前输入的内容。我如何解决它?

Scanner input = new Scanner(System.in); 

ArrayList arrayListOne;
arrayListOne = new ArrayList();
ArrayList<String> letterArray = new ArrayList<String>();
for(int i = 0; i < letterArray.size(); i++)  {
    System.out.println(letterArray.get(i));
}

System.out.println("Type a string:");
letterArray.add(input.nextLine());
System.out.println("Number of string in array: " + letterArray.size());

回答by Hovercraft Full Of Eels

You only add one String to the ArrayList. If you want it to display many Strings, first you need to add multiple Strings, probably in some sort of loop such as a for loop or while loop. Then after adding all text, create another for loop to display it all.

您只需将一个字符串添加到 ArrayList。如果您希望它显示多个字符串,首先您需要添加多个字符串,可能是在某种循环中,例如 for 循环或 while 循环。然后在添加所有文本后,创建另一个 for 循环以显示所有文本。

e.g. since this sounds like homework, much better to show what I mean in pseudo code (and shame to anyone who cheats you out of the experience of trying to code this yourself by spoon-feeding you a solution):

例如,因为这听起来像是家庭作业,所以最好用伪代码来展示我的意思(并且对于那些通过用勺子喂你解决方案来欺骗你自己进行编码的经验的人来说是耻辱):

create array list
entry String equals ""
do this loop
   get input from user
   put it into entry String
   add entry String into array list
while entry String doesn't equal "quit"
for each item in array list
   println each item
end for loop

回答by smilyface

private static List<String> getAndPrintInputFromUser() {

    Set<String> ids = new TreeSet<String>(); //used set for making list as unique
    Scanner input = new Scanner(System.in);
    System.out.println("\nEnter each value\n" +
            "and Put an extra ENTER .");
    do{

        String x = input.nextLine();

        if(x==null || StringUtils.isEmpty(x.trim())){
            break;
        }else {
            ids.add(x);
        }

    }while(true);

    for(String id : ids)
        System.out.println(id);

    return new ArrayList<String>(ids);
}

Output:

输出:

Enter each value
and Put an extra ENTER.
TP6100010
TP6100015
TP6100019


TP6100010
TP6100015
TP6100019

回答by Arijoon

Try the following to display the list and keep adding new letters:

尝试以下操作以显示列表并继续添加新字母:

Scanner input = new Scanner(System.in); 

ArrayList arrayListOne;
arrayListOne = new ArrayList();
ArrayList<String> letterArray = new ArrayList<String>();

while(true) {
  System.out.println("Type a string:");
  letterArray.add(input.nextLine());
  System.out.println("Number of string in array: " + letterArray.size());
  // Display eveything in the list
  displayList(letterArray);
}


// Somewhere in your class add this method
public void displayList(ArrayList letterArray) {
  for(int i = 0; i < letterArray.size(); i++) 
    System.out.println(letterArray.get(i));
}

full code:

完整代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Practice {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 

    ArrayList arrayListOne;
    arrayListOne = new ArrayList();
    ArrayList<String> letterArray = new ArrayList<String>();

    while(true) {
      System.out.println("Type a string:");
      letterArray.add(input.nextLine());
      System.out.println("Number of string in array: " + letterArray.size());
      // Display eveything in the list
      displayList(letterArray);
    }
  }


  public static void displayList(ArrayList letterArray) {
    for(int i = 0; i < letterArray.size(); i++) 
      System.out.println(letterArray.get(i));
  }
}