print arraylist element?

前端 未结 7 467
日久生厌
日久生厌 2021-01-11 13:59

how do i print the element \"e\" in arraylist \"list\" out?

 ArrayList list = new ArrayList();
 Dog e = new Dog();
 list.add(e);
 Syste         


        
7条回答
  •  长情又很酷
    2021-01-11 14:34

    Here is an updated solution for Java8, using lambdas and streams:

    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);
    

提交回复
热议问题