Java 打印数组列表元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2047003/
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
print arraylist element?
提问by ajsie
how do i print the element "e" in arraylist "list" out?
我如何打印出数组列表“列表”中的元素“e”?
ArrayList<Dog> list = new ArrayList<Dog>();
Dog e = new Dog();
list.add(e);
System.out.println(list);
采纳答案by sateesh
Do you want to print the entire list or you want to iterate through each element of the list? Either way to print anything meaningful your Dog
class need to override the toString()
method (as mentioned in other answers) from the Object
class to return a valid result.
您是要打印整个列表还是要遍历列表的每个元素?无论哪种方式打印任何有意义的Dog
类都需要覆盖类中的toString()
方法(如其他答案中所述)Object
以返回有效结果。
public class Print {
public static void main(final String[] args) {
List<Dog> list = new ArrayList<Dog>();
Dog e = new Dog("Tommy");
list.add(e);
list.add(new Dog("tiger"));
System.out.println(list);
for(Dog d:list) {
System.out.println(d);
// prints [Tommy, tiger]
}
}
private static class Dog {
private final String name;
public Dog(final String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
}
The output of this code is:
这段代码的输出是:
[Tommy, tiger]
Tommy
tiger
回答by Topher Fangio
Your code requires that the Dog
class has overridden the toString()
method so that it knows how to print itself out. Otherwise, your code looks correct.
您的代码要求Dog
该类已覆盖该toString()
方法,以便它知道如何打印自己。否则,您的代码看起来是正确的。
回答by Hyman
First make sure that Dog
class implements the method public String toString()
then use
首先确保Dog
该类实现了该方法,public String toString()
然后使用
System.out.println(list.get(index))
where index is the position inside the list. Of course since you provide your implementation you can decide how dog prints itself.
其中 index 是列表中的位置。当然,由于您提供了您的实现,您可以决定狗如何打印自己。
回答by GuruKulki
You should override toString()
method in your Dog
class. which will be called when you use this object in sysout.
你应该覆盖toString()
你的Dog
类中的方法。当您在 sysout 中使用此对象时将调用它。
回答by AJNeufeld
Here is an updated solution for Java8, using lambdas and streams:
这是 Java8 的更新解决方案,使用 lambdas 和流:
System.out.println(list.stream()
.map(Object::toString)
.collect(Collectors.joining("\n")));
Or, without joining the list into one large string:
或者,不将列表加入一个大字符串:
list.stream().forEach(System.out::println);
回答by Summved Jain
Printing a specific element is
打印特定元素是
list.get(INDEX)
I think the best way to print the whole list in one go and this will also avoid putting a loop
我认为最好的方法是一次性打印整个列表,这也将避免出现循环
Arrays.toString(list.toArray())
Arrays.toString(list.toArray())
回答by Prabhashani
If you want to print an arraylist with integer numbers, as an example you can use below code.
如果你想打印一个带有整数的数组列表,作为一个例子,你可以使用下面的代码。
class Test{
public static void main(String[] args){
ArrayList<Integer> arraylist = new ArrayList<Integer>();
for(int i=0; i<=10; i++){
arraylist .add(i);
}
for (Integer n : arraylist ){
System.out.println(n);
}
}
}
The output is above code:
输出是上面的代码:
0
1
2
3
4
5
6
7
8
9
10