In this example, I created an ArrayList<> of dogs instead of the usual Array[]. But, I want to know the best way to add the dog object to the ArrayList, and I also want to know the following:
1- How is ArrayList differ from Array? Which one is better?
2- Is it possible to use for loop to add the instantiated objects into the ArrayList?
import java.util.ArrayList;
import java.util.Collections;
public class DogDriver {
public static void main(String[] args) {
ArrayList<Dog> dogs = new ArrayList();
Dog dog1 = new Dog("Alex", "American Pit Bull Terrier", 100); //(name, breed, license)
Dog dog2 = new Dog("Duke", "German Shepherd", 113);
Dog dog3 = new Dog("Lucy", "Rottweiler", 120);
Dog dog4 = new Dog("Bailey", "Doberman Pinscher", 119);
Collections.sort(dogs);
for (Dog dog : dogs) {
System.out.println(dog);
}
}
}