Geting acces to method from class in arraylist

Em Eł :

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)

Arvind Kumar Avinash :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31409&siteId=1