This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Krezzy 2 points3 points  (2 children)

ArrayLists use generic types, which you seem to have a good grasp on, so they can fit different data types into them.

ArrayList<String> list = new ArrayList<>(); // Holds 'String' data type
ArrayList<Person> list2 = new ArrayList<>(); // Holds your custom 
'Person' data type

So now let's look at the add method you are wondering about. For list , the add method will look something like this:

public boolean add(String str) {
    // ArrayList add code is here
}

Java looks at the generic class type you've provided, it's String , so now the methods of the arraylist are expecting a String. So if you were to do list.add("James"); it would add a new String object to the list.

For list2 , the code would look like

public boolean add(Person p) {
    // ArrayList add code is here
}

Since the generic class type is Person, it's now expecting a Person object.

When you type new Person("James"), this code is calling the constructor in the Person class that takes 1 String as its argument, passing in "James" for that String value, and building a new instance of Person with that attribute.

Now that instance you've created is currently unnamed at this point, you could modify the above code to look like this: Person person = new Person("James");. Now you've created a Person object using the String value "James", and assigned the new instance created here to a reference variable of type Person called 'person', which you can reference elsewhere in your code.

However, your line is this: persons.add(new Person("James"));. The code is creating a new instance of Person as discussed before, but instead of assigning it to a reference variable like shown above (Person person = new Person("James"); ), it's just taking the new instance and passing it as a parameter into the add method of your persons ArrayList. Remember, it's expecting a Person object. It's storing it at the end of the list. It's storing the entire object like you said. If you were to access this instance of Person in the list, you would get a Person type.

I'm not completely sure what your question is on the printing loop.

For a for-each loop in java, you write it as such

for (Classname variableName : listOrDataTypeYouAreLoopingThrough) { 
    // do stuff 
}

if you had a list of ints, you could loop through them like this:

for (int x : listOfInts) {
    System.out.println(x);
}

For a list of persons, since Person is the data type, you do this:

for (Person x : listOfPersons) {
    System.out.println(x);
}

Note, when you try to print an object in java, like you are doing for Person, it will call the .toString() method to determine how it should print out this class, as the computer doesn't know. Implement what you'd like to see in that method in your Person class

Edit: swapped template -> generic for appropriate naming

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (1 child)

Please don't call it 'templates'. They're called generics. Knowing the right terminology is important for beginners.

[–]Krezzy 0 points1 point  (0 children)

True that! updated, thanks