【问题标题】:How to get a value inside an ArrayList java如何在ArrayList java中获取一个值
【发布时间】:2020-12-31 01:06:10
【问题描述】:

我正在尝试从 ArrayList 中获取值。这是我的代码示例:

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){

   // in heare i need a way of getting the total cost of all three cars by calling 
    //  computeCars ()
    System.out.println(cars.get());
}

修订 谢谢大家的回答,我可能应该多加一点代码。在 Car 类中,我有另一种方法来计算包括税在内的总成本。

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 );
     } 
}

在主类中

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?
    }
}

再次感谢

【问题讨论】:

    标签: java


    【解决方案1】:

    假设你的 Car 类有一个获取价格的方法,你可以简单地使用

    System.out.println (car.get(i).getPrice());
    

    其中i 是元素的索引。

    你也可以使用

    Car c = car.get(i);
    System.out.println (c.getPrice());
    

    如果需要存储,还需要从函数中返回totalprice

    主要

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

    并更改函数的 return 类型

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

    【讨论】:

    • 嗨,谢谢你的帮助,我试过了,但它说它期待一个 int 但发现 void。我不知道如何解决这个问题。再次感谢
    【解决方案2】:

    您尚未显示您的 Car 类型,但假设您想要第一辆车的价格,您可以使用:

    public static void processCars(ArrayList<Car> cars) {
        Car car = cars.get(0);
        System.out.println(car.getPrice());
    }
    

    请注意,我已将列表名称从 car 更改为 cars - 这是一个列表汽车,而不是单个汽车。 (我以类似的方式更改了方法名称。)

    如果您只希望该方法处理单个汽车,则应将参数更改为Car类型:

    public static void processCar(Car car)
    

    然后这样称呼它:

    // In the main method
    processCar(cars.get(0));
    

    如果您确实将其保留为处理整个列表,则值得将参数推广到 List&lt;Car&gt; - 您不太可能真的要求它是 ArrayList&lt;Car&gt;

    【讨论】:

    • ArrayList 中的类型应该是 Car,而不是 Cars ;-)
    • @stiank81:我认为您的意思是针对家庭答案的评论,而不是我的...
    • 其实你的也是错的,但Petar编辑了它..不过没什么大不了的..
    【解决方案3】:

    主类

    public class Test {
        public static void main (String [] args){
            Car thisCar= new Car ("Toyota", "$10000", "2003");  
            ArrayList<Car> car= new ArrayList<Car> ();
            car.add(thisCar); 
            processCar(car);
        } 
    
        public static void processCar(ArrayList<Car> car){
            for(Car c : car){
                System.out.println (c.getPrice());
            }
        }
    }
    

    汽车类

    public class Car {
        private String vehicle;
        private String price;
        private String model;
    
        public Car(String vehicle, String price, String model){
            this.vehicle = vehicle;
            this.model = model;
            this.price = price;
        }
    
        public String getVehicle() {
            return vehicle;
        }
    
        public String getPrice() {
            return price;
        }
    
        public String getModel() {
            return model;
        }  
    }
    

    【讨论】:

      【解决方案4】:

      您应该将列表命名为 cars 而不是 car,以便其名称与其内容匹配。

      那么你可以简单地说cars.get(0).getPrice()。如果你的Car 类还没有这个方法,你需要创建它。

      【讨论】:

        【解决方案5】:

        列表可能包含多个元素,因此 get 方法需要一个参数:您要检索的元素的索引。如果你想要第一个,那就是 0。

        列表包含 Car 实例,所以你只需要这样做

        Car firstCar = car.get(0);
        String price = firstCar.getPrice();
        

        或者只是

        String price = car.get(0).getPrice();
        

        car 变量应命名为 cars,因为它是一个列表,因此包含多辆汽车。

        阅读tutorial about collections。并学会使用javadoc:所有的类和方法都有文档记录。

        【讨论】:

          【解决方案6】:

          如果你想获得所有汽车的价格,你必须遍历数组列表:

          public static void processCars(ArrayList<Cars> cars) {
             for (Car c : cars) {
                 System.out.println (c.getPrice());
             }
          }
          

          【讨论】:

            【解决方案7】:
            import java.util.*;
            import java.lang.*;
            import java.io.*;
            
            class hari
            {
            public static void main (String[] args) throws Exception
            {  Scanner s=new Scanner(System.in);
                int i=0;
                int b=0;
                HashSet<Integer> h=new HashSet<Integer>();
                try{
                    for(i=0;i<1000;i++)
                {   b=s.nextInt();
                    h.add(b);
                }
                }
                catch(Exception e){
                    System.out.println(h+","+h.size());
                }
            }}
            

            【讨论】:

            • 这个解决方案非常适合你的问题......我希望
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多