[java] How to display all elements in an arraylist?

Say I have a car class with attributes make and registration, and i create an ArrayList to store them. How do I display all the elements in the ArrayList?

I have this code right now:

public Car getAll()
{
    for(int i = 0; i < cars.size(); i++) //cars name of arraylist
    {
        Car car = cars.get(i);  
        {
            return cars.get (i);
        }
    }
    return null;
}

It compiles fine but when I try it out in my tester class using this code:

private static void getAll(Car c1)
{
    ArrayList <Car> cars = c1.getAll(); // error incompatible type
    for(Car item : cars)
    {   
        System.out.println(item.getMake()
                + " "
                + item.getReg()
                );
    }
}

I am getting a error of incompatible type. Is my coding correct? If not can someone please show me how it should be?

Thank You

This question is related to java arraylist car-analogy

The answer is


Tangential: String.format() rocks:

public String toString() {
    return String.format("%s %s", getMake(), getReg());
}

private static void printAll() {
    for (Car car: cars)
        System.out.println(car); // invokes Car.toString()
}

Set for each loop to get all values

for (String member : members){
    Log.i("Member name: ", member);
}

Another approach is to add a toString() method to your Car class and just let the toString() method of ArrayList do all the work.

@Override
public String toString()
{
    return "Car{" +
            "make=" + make +
            ", registration='" + registration + '\'' +
            '}';
}

You don't get one car per line in the output, but it is quick and easy if you just want to see what is in the array.

List<Car> cars = c1.getAll();
System.out.println(cars);

Output would be something like this:

[Car{make=FORD, registration='ABC 123'},
Car{make=TOYOTA, registration='ZYZ 999'}]

Are you trying to make something like this?

public List<Car> getAll() {
    return new ArrayList<Car>(cars);
}

And then calling it:

List<Car> cars = c1.getAll();
for (Car item : cars) {   
    System.out.println(item.getMake() + " " + item.getReg());
}

Arraylist uses Iterator interface to traverse the elements Use this

public void display(ArrayList<Integer> v) {
        Iterator vEnum = v.iterator();
        System.out.println("\nElements in vector:");
        while (vEnum.hasNext()) {
            System.out.print(vEnum.next() + " ");
        }
    }

You are getting an error because your getAll function in the Car class returns a single Car and you want to assign it into an array.

It's really not clear and you may want to post more code. why are you passing a single Car to the function? What is the meaning of calling getAll on a Car.


Hi sorry the code for the second one should be:

private static void getAll(CarList c1) {

ArrayList <Car> cars = c1.getAll(); // error incompatible type
for(Car item : cars)
{   
      System.out.println(item.getMake()
                       + " "
                       + item.getReg()
                       );
}

}

I have a class called CarList which contains the arraylist and its method, so in the tester class, i have basically this code to use that CarList class:

CarList c1; c1 = new CarList();

everything else works, such as adding and removing cars and displaying an inidividual car, i just need a code to display all cars in the arraylist.


It's not at all clear what you're up to. Your function getAll() should return a List<Car>, not a Car. Otherwise, why call it getAll?

If you have

Car[] arrayOfCars

and want a List, you can simply do this:

List<Car> listOfCars = Arrays.asList(arrayOfCars);

Arrays is documented Here.


You can use arraylistname.clone()


Your getAll() method does not get all. It returns the first car.

The return statement terminates the loop.