के अंदर कोई मान कैसे प्राप्त करें I ArrayList में से कोई मान प्राप्त करने का प्रयास कर रहा हूं। यहाँ मेरी कोड का एक नमूना है:एक 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());
}
संशोधन धन्यवाद सभी प्रश्नों के उत्तर के लिए, मैं शायद कोड में थोड़ा और अधिक जोड़ने चाहिए। कार वर्ग में, मेरे पास एक और तरीका है जो कर सहित कुल लागत की गणना कर रहा है।
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?
}
}
धन्यवाद फिर से
नमस्ते, मदद के लिए धन्यवाद, मैंने कोशिश की लेकिन यह कह रहा है कि यह एक int की उम्मीद है लेकिन शून्य पाया गया है। मुझे यकीन नहीं है कि इसके आसपास कैसे जाना है। एक बार फिर धन्यवाद – blackStar