Java的数组列表,用于存储userinput [英] java arraylist that stores userinput

查看:242
本文介绍了Java的数组列表,用于存储userinput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能得到它回去当用户类型是还怎么做我打印出来的ArrayList使用打印格式启动另一个目录

感谢

 进口的java.util.ArrayList;
进口java.util.Scanner中;公共AAA级
{
    公共静态无效的主要(字串[] args)
{    ArrayList的<串GT; NAME =新的ArrayList<串GT;();
    ArrayList的<整数GT;手机=新的ArrayList<整数GT;();
    扫描仪扫描=新的扫描仪(System.in);
    {
        的System.out.println(请输入您的姓名:);
        name.add(scanner.next());
        的System.out.println(请输入您的电话号码:);
        phone.add(scanner.nextInt());        的System.out.println(你想添加一个目录是/否?);
        字符串的答案= scanner.nextLine();        如果(answer.equals(是));
        //希望它回到这里启动另一个direcotry        其他
    {        的System.out.println(感谢添加到目录);
        的System.out.println(回答());
    }
    }    }
}


解决方案

您可以轻松打印出一个ArrayList

 的System.out.println(名单);

这将打印出类似这样:

[一,二,三]

,其中一,二,三都的toString调用()或数组列表中的元素的结果。

您可以通过删除[]轻松:

 的System.out.println(list.toString()代替([,).replace(],));

您需要使用您的自定义格式实现循环打印:

 的(一个String:名单){
    的System.out.println(格式(S));
}

how can I get it to go back to start another directory when the user types yes and also how do I print the arraylist out using a print format

thanks

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

public class AAA
{
    public static void main(String[] args) 
{

    ArrayList<String> name = new ArrayList<String>();
    ArrayList<Integer> phone = new ArrayList<Integer>(); 
    Scanner scanner = new Scanner(System.in); 


    {
        System.out.println("Please enter your name: ");
        name.add(scanner.next());
        System.out.println("Please enter your number: ");
        phone.add(scanner.nextInt());

        System.out.println("Do you want to add a directory yes/no?");
        String answer = scanner.nextLine(); 

        if (answer.equals("yes"));
        //want it to go back to start another direcotry here

        else 
    {

        System.out.println("Thanks for adding to the directory");
        System.out.println(answer());
    }


    }

    }
}

解决方案

You can easily print out an ArrayList:

System.out.println(list);

It will print something like:

[one, two, three]

where one, two, three are the results of invocation of toString() or array list's elements.

You can remove [] easily by:

System.out.println(list.toString().replace("[", "").replace("]", ""));

You you need to use your custom format implement print in loop:

for (String s : list) {
    System.out.println(format(s));
}

这篇关于Java的数组列表,用于存储userinput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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