2

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);
        }
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
askd
  • 71
  • 10
  • 1
    dogs.add(dog1); http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(E) – kosa Feb 24 '15 at 20:44
  • 4
    I'd recommend you to consult reading material on java instead. This question does not really show any research effort as it stands. – Cubic Feb 24 '15 at 20:45
  • http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line – Michael Welch Feb 24 '15 at 20:46

2 Answers2

6

Right now, dogs is empty. This means that sort has no effect, and the enhanced-for will never execute.

You have several options:

  • Add them when you instantiate them. Don't create variables that aren't going to be used.

    As an example, one of your lines would read instead:

    dogs.add(new Dog("Alex", "American Pit Bull Terrier", 100));
    
  • Use a static initializer for the ArrayList. This is a wee bit harder to read, but it's pretty slick. It accomplishes the same thing as doing the straight-up add, but in a different location.

    ArrayList<Dog> dogs = new ArrayList<Dog>() {{
        add(new Dog("Alex", "American Pit Bull Terrier", 100));
        add(new Dog("Duke", "German Shepherd", 113));
        add(new Dog("Lucy", "Rottweiler", 120));
        add(new Dog("Bailey", "Doberman Pinscher", 119));
    }};
    
  • As suggested in comments, use Arrays.asList():

    ArrayList<Dog> dogs = new ArrayList<>(Arrays.asList(dog1, dog2, dog3, dog4, dog5));
    

    This does require the variables, though.

To the other questions you've posed:

  • Each has their purpose and place. Use an array when you have a fixed length of objects to work with, and use a List of any kind (ArrayList, LinkedList, etc) when you don't have a fixed length.

  • As I explained above, no. The enhanced-for iterates over the existing elements in the collection, and since there are no elements in the collection, the loop will not execute.

    It'd be a bit awkward to do it in a loop unless you had the data you wanted in another structure that could be indexed into, so I wouldn't recommend that approach.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Instead of using the static initializer approach, you'd probably have a simpler time doing `new ArrayList<>(Arrays.asList(...))` – Louis Wasserman Feb 24 '15 at 20:58
2
  1. Main difference is that after filling arraylist with data it is going to extend, and you'll be able to add more data. In array when it is being filled, and you try to add more data it throws an exception. About performance: it depends... but usually normal array.
  2. Yes, use arraylistname.add(objectname);
Galunid
  • 578
  • 6
  • 14