Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印ArrayList对象_Java_Arraylist - Fatal编程技术网

Java 打印ArrayList对象

Java 打印ArrayList对象,java,arraylist,Java,Arraylist,我已经创建了几个对象,我只想打印ArrayList中对象的一个参数: Superhero batman = new Superhero("Bruce", 26, "Batman"); Human rachel = new Human("Rachel", 24); Superhero ironman = new Superhero("Tony", 35, "Ironman"); Human pepper = new Human("Pepper", 22); List<Human>

我已经创建了几个对象,我只想打印ArrayList中对象的一个参数:

Superhero batman = new Superhero("Bruce", 26, "Batman");
Human rachel = new Human("Rachel", 24);

Superhero ironman = new Superhero("Tony", 35, "Ironman");
Human pepper = new Human("Pepper", 22);

List<Human> people = new ArrayList<Human>();
people.add(batman);
people.add(rachel);
people.add(ironman);
people.add(pepper);
这将打印人员列表中每个人的姓名。您应该在Human类中具有以下方法:

getName() {
    return this.name;
}
Java 8之前

for (Human human : people) {
    System.out.println(human.getName());
}
从Java8开始

people.stream().map(Human::getName).forEach(System.out::println);

迭代列表中的人(每次迭代访问每个人),为每个人获取其名称并打印。如果您对此解决方案有问题,请询问更具体的问题。请用您的问题展示您解决此问题的尝试。若要摆脱Pshemo所说的,请遍历人员列表,并获取对象名称
for(Human-Human:people){System.out.println(Human.getName());}
将来,请用您的问题展示您真诚的尝试——否则我们不知道您做错了什么,否则您的问题读起来像是在乞求代码。对不起,我下次再问
for (Human human : people) {
    System.out.println(human.getName());
}
people.stream().map(Human::getName).forEach(System.out::println);