
Question:
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
Solution:1
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()); }
Solution:2
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.
Solution:3
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.
Solution:4
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() }
Solution:5
Your getAll()
method does not get all. It returns the first car.
The return
statement terminates the loop.
Solution:6
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'}]
Solution:7
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.
Solution:8
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() + " "); } }
Solution:9
You can use arraylistname.clone()
Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
EmoticonEmoticon