Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value inside an ArrayList java

Tags:

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

like image 852
blackStar Avatar asked Oct 31 '11 07:10

blackStar


People also ask

How do you get a particular value from an ArrayList?

The get() method of the ArrayList class accepts an integer representing the index value and, returns the element of the current ArrayList object at the specified index. Therefore, if you pass 0 to this method you can get the first element of the current ArrayList and, if you pass list.

How do you find a specific value in a list Java?

To find an element matching specific criteria in a given list, we: invoke stream() on the list. call the filter() method with a proper Predicate. call the findAny() construct, which returns the first element that matches the filter predicate wrapped in an Optional if such an element exists.

What does get () do in Java?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array.

How can I search the element from ArrayList in Java?

An element in an ArrayList can be searched using the method java. util. ArrayList. indexOf().


1 Answers

Assuming your Car class has a getter method for price, you can simply use

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

where i is the index of the element.

You can also use

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

You also need to return totalprice from your function if you need to store it

main

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

And change the return type of your function

public int computeCars (){
    int  totalprice= price+tax;
    System.out.println (name + "\t" +totalprice+"\t"+year );
    return  totalprice; 
 }
like image 188
Ankur Avatar answered Sep 21 '22 13:09

Ankur