Java 如何在数组列表上打印单个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22023053/
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
How do I print a Single item on a Array List
提问by Brendan ten
So I want to system print one of the names on my array list I was thinking it's like System.out.print(Names[1]) for the second item on my array list?
所以我想系统打印我的数组列表中的一个名称我认为它就像 System.out.print(Names[1]) 对于我的数组列表中的第二个项目?
System.out.println("How Many Employees On your Payroll");
Employees = reader.nextDouble();
ArrayList Names = new ArrayList();
for (int i=1;i<=Employees;i++)
{
System.out.println("What is the name for Employee #"+i+"");
String userName = userInputScanner.nextLine();
Names.add(userName);
}
System.out.println("Do these Names Seem Correct?: " + Names);
采纳答案by Alex Kartishev
First of all try to obey Java code conventions: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
首先尽量遵守Java代码约定:http: //www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
And to get the second element of the list use
并获取列表的第二个元素使用
names.get(1);
Where names
is your list.
names
你的清单在哪里。
回答by Eng.Fouad
The JavaDocis your friend. Use ArrayList#get(int index)
.
该JavaDoc的是你的朋友。使用ArrayList#get(int index)
.
回答by mok
First, ArrayList is a generic type so it's better to use ArrayList , for example:
首先, ArrayList 是一个泛型类型,所以最好使用 ArrayList ,例如:
ArrayList<String> Names = new ArrayLis<String> ();
Second,You can access the i'th number of an ArrayList (Say Names) by "get(int index)" method:
其次,您可以通过“get(int index)”方法访问 ArrayList (Say Names) 的第 i 个数字:
Names.get(1)//to get the second items