Java ArrayList,在一行中接受多种类型(int、String 等)的用户输入 [英] Java ArrayList, taking user input of multiple types(int, String etc.) in one line

查看:29
本文介绍了Java ArrayList,在一行中接受多种类型(int、String 等)的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在 Java 方面做得更好,我遇到的一个问题是接收用户输入,就像这样:

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:

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、狗、兔子、7、#

1, 2, 4, 257, dog, rabbit, 7, #

甚至

1 2 4 257 狗兔 7 #

1 2 4 257 dog rabbit 7 #

我曾在多个地方看到如何一次读取一个输入,但我不确定一次性读取动态 ArrayList 的最佳方法.

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.

推荐答案

试试这个简单的例子来打印数组列表值

try this simple example to print the arraylist values

     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);

            }

        }

这篇关于Java ArrayList,在一行中接受多种类型(int、String 等)的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆