Получение значения из ArrayList java

#java

#java

Вопрос:

Я пытаюсь получить значение из with в an ArrayList . У меня есть два класса: main и класс Car. вот код:

   public class CarOrders {
     public static void main (String [] args){
        Car toyota= new Car("Toyota", "$10000", "300"  "2003");
        Car nissan= new Car("Nissan", "$22000", "300"  "2011");
        Car ford= new Car("Ford", "$15000", "350"  "2010");

        ArrayList<Car> cars = new ArrayList<Car>();
        cars.add(toyota);        
        cars.add(nissan);
        cars.add(ford);
     }

public static void processCar(ArrayList<Car> cars){
       int totalAmount=0;
       for (int i=0; i<cars.size(); i  ){
          cars.get(i).computeCars ();
         totalAmount = ?? 
      // in need to add the computed values of totalprice from the Car class?

       }
      System.out.println (totalAmount);

    }


}    
class Car {
     public Car (String name, int price, int, tax, int year)
     {
       constructor.......
     }

     public void computeCars ()
     {
      int  totalprice= price tax;
      System.out.println (name   "t"  totalprice "t" year );
     }

}
 

Как я смогу вычислить totalAmount в processCar() методе where totalAmount=totalAmount totalPrice из computCar() метода в классе Car?

Комментарии:

1. Сделайте computeCars() возврат totalprice . Таким образом totalAmount = cars.get(i).computeCars () ;

Ответ №1:

Просто return price tax; из computeCars() :

  public int computeCars ()
 {
  return price tax;
 }
 

затем :

     public static void processCar(ArrayList<Car> cars){
       int totalAmount=0;
       for (int i=0; i<cars.size(); i  ){
         totalAmount = cars.get(i).computeCars(); 
       }
      System.out.println (totalAmount);
    }
 

Ответ №2:

Я бы посоветовал вам изменить сигнатуру вашего computCar() метода

 public int computeCar() { ... }
 

и возвращает значение totalPrice as из этого вызова метода. Таким образом, вы можете использовать его в своем методе processCar() .

Ответ №3:

 public int  computeCars ()
     {
      return price tax;

     }


public static void processCar(ArrayList<Car> cars){
       int totalAmount=0;
       for (int i=0; i<cars.size(); i  ){

         totalAmount=  cars.get(i).computeCars ();
      // in need to add the computed values of totalprice from the Car class?

       }
      System.out.println (totalAmount);

    }
 

Ответ №4:

Вам нужно внести некоторые изменения в свой класс Car, это должно быть так:

 class Car {
 //i'm adding only 2 properties you can add all the properties
 public int price;
 public int tax;
 public Car (String name, int price, int tax, int year)
 {
   //here you should add these 2 lines
   this.price=price;
   this.tax=tax;
 }
 

и ваша функция будет:

   public static void processCar(ArrayList<Car> cars){
   int totalAmount=0;
   for (int i=0; i<cars.size(); i  ){
      cars.get(i).computeCars ();
     totalAmount=  cars.get(i).price;
  // in need to add the computed values of totalprice from the Car class?

   }
  System.out.println (totalAmount);

}