Java ArrayList,在一行中接受多种类型(int、String 等)的用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18808068/
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
Java ArrayList, taking user input of multiple types(int, String etc.) in one line
提问by trueCamelType
I'm working on getting a little better at Java, and a problem I've run into is taking user input, all in one line like this:
我正在努力在 Java 方面做得更好,我遇到的一个问题是接收用户输入,就像这样:
System.out.println("Please input numbers that you would like to work with");
//Read in user input into ArrayList, taking into account that they may input Strings or anything else.
Assuming the user inputs something like this
假设用户输入这样的东西
1, 2, 4, 257, dog, rabbit, 7, #
1, 2, 4, 257, 狗, 兔子, 7, #
or even
甚至
1 2 4 257 dog rabbit 7 #
1 2 4 257 狗兔 7 #
I've seen in several places how to read in one input at a time, but I wasn't sure of the best way to read in a dynamic ArrayList all at once.
我已经在几个地方看到如何一次读取一个输入,但我不确定一次读取动态 ArrayList 的最佳方法。
I'm not really concerned with the difference in doing it with commas or without commas since logically I think I know how to do that, and haven't tried yet, so really the main problem is as stated above (reading user input into ArrayList of dynamic size when user inputs all numbers at once). Thanks, and I'm not necessarily looking for code, this isn't homework, just wondering best way to do this. Just stating logically how it's done will work, but code is appreciated.
我并不真正关心使用逗号或不使用逗号的区别,因为从逻辑上讲,我认为我知道该怎么做,并且还没有尝试过,所以实际上主要问题如上所述(将用户输入读入 ArrayList当用户一次输入所有数字时的动态大小)。谢谢,我不一定要寻找代码,这不是家庭作业,只是想知道最好的方法。只是从逻辑上说明它是如何完成的就可以了,但是代码很受欢迎。
采纳答案by Nambi
try this simple example to print the arraylist values
试试这个简单的例子来打印 arraylist 值
import java.util.*;
class SimpleArrayList{
public static void main(String args[]){
List l=new ArrayList();
System.out.println("Enter the input");
Scanner input=new Scanner(System.in);
String a =input.nextLine();
l.add(a);
// use this to iterate the value inside the arraylist.
/* for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
} */
System.out.println(l);
}
}
回答by false_memories
One approach is to tokenize the input and then add it into an array like this:
一种方法是对输入进行标记,然后将其添加到数组中,如下所示:
Scanner scn = new Scanner(System.in);
System.out.println("Put in a set: ");
String input = scn.nextLine();
System.out.println(input);
Scanner tokenizer = new Scanner(input);
tokenizer.useDelimiter(" ");
ArrayList<Object> arr = new ArrayList<Object>();
while(tokenizer.hasNext())
{
arr.add(tokenizer.next());
System.out.println(arr.get(arr.size()-1));
}
System.out.println(arr);
回答by Sir RotN
As I think there are enough answers on how to read data from System.in
, I'll take a different approach here. First you should be aware that this is not the major way of getting data in java. In fact in more than 10 years I never used it. That's why there's no complete ready to use solution for give me the stuctured data into some container (like ArrayList). Instead you get simply one string per line. And you have to deal with that on your own. this process is called parsing. Depending on the complexity of the chosen syntax there are several approaches like using a parser generator if it's more complex or write the parser by hand in simpler case. I'd like to get into your first suggestion and describe it as comma separated with optional whitespace. For a syntax like this the class Scanner delivers quite some support. Numbers can be recognized and the tokenizing is done almost automatic. However, if you have more specific data you might need some aditional effort, like I demonstrated with a map of animals I used to convert that very special data type. To be flexible enough to solve all the real world problems there can't be a ready to use solution. Only comprehensive support to build your own.
因为我认为关于如何从中读取数据有足够的答案 System.in
,我会在这里采取不同的方法。首先,您应该意识到这不是在 Java 中获取数据的主要方式。事实上,十多年来我从未使用过它。这就是为什么没有完整的即用型解决方案将结构化数据提供到某个容器(如 ArrayList)中。相反,您每行只得到一个字符串。你必须自己处理。这个过程称为解析。根据所选语法的复杂性,有几种方法,例如在更复杂的情况下使用解析器生成器或在更简单的情况下手动编写解析器。我想了解您的第一个建议,并将其描述为用可选空格分隔的逗号。对于这样的语法,类 Scanner 提供了相当多的支持。可以识别数字,并且标记化几乎是自动完成的。然而,如果你有更具体的数据,你可能需要一些额外的努力,就像我用动物地图演示的那样,我用来转换这种非常特殊的数据类型。为了足够灵活以解决所有现实世界的问题,不可能有现成的解决方案。只有全面支持才能构建您自己的。
Map<String, Animal> animals = ...
Scanner scanner = new Scanner("1, 2, 4, 257, dog, rabbit, 7, #").useDelimiter(",");
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
result.add(scanner.nextInt());
} else {
String val = scanner.next();
if (animals.containsKey(val)) {
result.add(animals.get(val));
} else {
result.add(val);
}
}
}
回答by Rakesh Divedi
you can try this code for taking input dinamically in arraylist and store in arraylist
您可以尝试使用此代码在数组列表中动态获取输入并存储在数组列表中
import java.util.ArrayList;
import java.util.Scanner;
public class HelloWorld
{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
String j;
ArrayList l=new ArrayList();
for(int i=0;i<6;i++)
{ j=sc.nextLine();
l.add(j);
}
System.out.println("Hello World"+l);
}
}