Em Eł
Em Eł

Reputation: 65

Geting acces to method from class in arraylist

how can i get access to method in class from arraylist?

ArrayList car = new ArrayList<>;
car.add(new carClass("Audi", 35000));
System.out.println("The price is:"+ car.get(0) );

i woud like to get only price from this arraylist (method getPrice)

Upvotes: 1

Views: 42

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79025

First of all, replace

ArrayList car = new ArrayList<>;

with

List<Car> carList = new ArrayList<>();

and then do as follows:

System.out.println("The price is:"+ carList.get(0).getPrice());

Check this for more understanding on this topic.

Finally, you should follow the Java naming convention e.g. carClass should be CarClass or simply, Car.

Upvotes: 2

Related Questions