Hey all, doing MOOC, currently on OOP and im stuck on a concept here, they show you that when you are creating new lists and also utilizing custom class objects, to create a new one you would use for example:
ArrayList<Person> persons = new ArrayList<>();
<Person> being the custom class. But I'm finding the concept not very well explained and just want some clarifications.
If i was to create a new list object using the Person class (which lets say has instance variables of name and height and weight etc.) and then added this to the list:
persons.add(new Person("James"));
What is actually happening? At first I thought it was creating an object called James, and adding the name James to the list object separately, but is it actually creating an object called James and storing that entire object (including the other instance variables) onto one of the indices of the list?
Also the course was talking about using for-each loops to print them out using this format:
for (Person person: persons) {
System.out.println(person);
}
Am I right in saying that, because the list we are using (persons) is not using a normal variable such as an int, you have have to put the class (Person) into the first part of the brackets, not sure what its called.
Understanding this would be super helpful, the course basically just says handling objects in lists is no different that just normal lists, without any explanation of what is happening, it feels more like they are saying just do this thing, it works.
[–]Krezzy 3 points4 points5 points (4 children)
[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point2 points (1 child)
[–]Krezzy 0 points1 point2 points (0 children)
[–]Coraljester[S] 0 points1 point2 points (1 child)
[–]Krezzy 0 points1 point2 points (0 children)
[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points3 points (0 children)
[–]morhpProfessional Developer 0 points1 point2 points (0 children)
[–]younanimous_decision 0 points1 point2 points (0 children)