How to print out all the elements of a List in Java?
java, list
Solution
Here is some example about getting print out the list component:
public class ListExample {
public static void main(String[] args) {
List<Model> models = new ArrayList<>();
// TODO: First create your model and add to models ArrayList, to prevent NullPointerException for trying this example
// Print the name from the list....
for(Model model : models) {
System.out.println(model.getName());
}
// Or like this...
for(int i = 0; i < models.size(); i++) {
System.out.println(models.get(i).getName());
}
}
}
class Model {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Problem
I am trying to print out all the elements of a `List`, however it is printing the pointer of the `Object` rather than the value. This is my printing code... ``` for(int i=0;i<list.size();i++){ System.out.println(list.get(i)); } ``` Could anyone please help me why it isn't printing the value of the elements.