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