[java] How to print out all the elements of a List in Java?

  1. You haven't specified what kind of elements the list contains, if it is a primitive data type then you can print out the elements.
  2. But if the elements are objects then as Kshitij Mehta mentioned you need to implement (override) the method "toString" within that object - if it is not already implemented - and let it return something meaning full from within the object, example:

    class Person {
        private String firstName;
        private String lastName;
    
        @Override
        public String toString() {
            return this.firstName + " " + this.lastName;
        }
    }