用于存储用户输入的 Java Arraylist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4455873/
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 to store user input
提问by George
Hi I am new to arraylists and java and I was wondering if someone could help me or give me pointers on how to create a program that allows the user to repeatedly enter directory entries from the keyboard and store them in an arraylist.
嗨,我是 arraylists 和 java 的新手,我想知道是否有人可以帮助我或指导我如何创建一个程序,该程序允许用户从键盘重复输入目录条目并将它们存储在 arraylist 中。
enter name:
enter telephone number:
and then ask if the user wants to enter another one
然后询问用户是否要输入另一个
enter another: Y/N
thanks
谢谢
采纳答案by Bad Request
You can still use two ArrayLists, or make a class with name and phone attributes and then make one ArrayList of objects of that class.
您仍然可以使用两个 ArrayList,或者创建一个具有 name 和 phone 属性的类,然后创建该类的对象的一个 ArrayList。
First approach shown here.
此处显示的第一种方法。
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 sc = new Scanner(System.in);
while (true) {
System.out.println("Please enter your name: ");
name.add(sc.next());
System.out.println("Please enter your number: ");
phone.add(sc.nextInt());
}
}
}
回答by Chris
回答by Ratna Dinakar
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Tester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> directoryNames= new ArrayList<String>();
String input=getDirectoryName();
String directoryPath="";
String userChoice="";
String[] inputTokens=input.split(" ");
if(inputTokens.length>1)
{
directoryPath=inputTokens[0];
userChoice=inputTokens[1];
}
else
{
directoryPath=inputTokens[0];
}
while(!"q".equalsIgnoreCase(userChoice))
{
directoryNames.add(directoryPath);
input=getDirectoryName();
inputTokens=input.split(" ");
if(inputTokens.length>1)
{
directoryPath=inputTokens[0];
userChoice=inputTokens[1];
}
else
{
directoryPath=inputTokens[0];
}
}
}
public static String getDirectoryName()
{
String input="";
System.out.println("Please Enter Directory name . If you want to quit press q or Q at the end of directory name \n ");
System.out.println("\n Example <directory_path> q");
Scanner in = new Scanner(System.in);
input=in.nextLine().trim();
return input;
}
}
回答by Zoyeb Shaikh
import java.util.*;
class simple
{
public static void main(String args[])
{
ArrayList<String> al=new ArrayList<String>();
ArrayList<Integer> al1=new ArrayList<Integer>();
Scanner ac=new Scanner(System.in);
al.add(ac.next());
al1.add(ac.nextInt());
Iterator itr=al.iterator();
Iterator itr1=al1.iterator();
while(itr.hasNext()&& itr1.hasNext())
{
System.out.println(itr.next());
System.out.println(itr1.next());
}
}
}