[java] How to get a value inside an ArrayList java

I am trying to get a value from with in an ArrayList. Here is a sample of my code:

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

revision thanks all for the answers, I should probably add to the code a bit more. in the Car class, i have another method that is calculating the total cost including the tax.

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

in the main class

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

Thanks again

This question is related to java

The answer is


You should name your list cars instead of car, so that its name matches its content.

Then you can simply say cars.get(0).getPrice(). And if your Car class doesn't have this method yet, you need to create it.


You haven't shown your Car type, but assuming you'd want the price of the first car, you could use:

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

Note that I've changed the name of the list from car to cars - this is a list of cars, not a single car. (I've changed the method name in a similar way.)

If you only want the method to process a single car, you should change the parameter to be of type Car:

public static void processCar(Car car)

and then call it like this:

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

If you do leave it as processing the whole list, it would be worth generalizing the parameter to List<Car> - it's unlikely that you'll really require that it's an ArrayList<Car>.


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

If you want to get the price of all cars you have to iterate through the array list:

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

The list may contain several elements, so the get method takes an argument : the index of the element you want to retrieve. If you want the first one, then it's 0.

The list contains Car instances, so you just have to do

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

or just

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

The car variable should be named cars, since it's a list and thus contains several cars.

Read the tutorial about collections. And learn to use the javadoc: all the classes and methods are documented.


main class

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

car class

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