如何在ArrayList java中获取值
我正在尝试从ArrayList中的with获取值。以下是我的代码示例:如何在ArrayList java中获取值,java,Java,我正在尝试从ArrayList中的with获取值。以下是我的代码示例: 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
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());
}
在班上
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?
}
}
publicstaticvoidprocesscar(arraylistcars){
int totalAmount=0;
对于(int i=0;i您尚未显示您的汽车类型,但假设您想要第一辆汽车的价格,您可以使用:
public static void processCars(ArrayList<Car> cars) {
Car car = cars.get(0);
System.out.println(car.getPrice());
}
然后这样称呼它:
// In the main method
processCar(cars.get(0));
如果您将它作为处理整个列表的一部分,那么值得将参数泛化为列表
——您不太可能真正要求它是数组列表
,假设您的汽车
类有一个价格的getter方法,您可以简单地使用
System.out.println (car.get(i).getPrice());
其中i
是元素的索引
你也可以使用
Car c = car.get(i);
System.out.println (c.getPrice());
如果需要存储,还需要从函数返回totalprice
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;
}
}
该列表可能包含多个元素,因此get方法采用一个参数:要检索的元素的索引。如果要检索第一个元素,则该参数为0
该列表包含汽车实例,因此您只需执行以下操作
Car firstCar = car.get(0);
String price = firstCar.getPrice();
或者只是
String price = car.get(0).getPrice();
car
变量应命名为cars
,因为它是一个列表,因此包含多个cars
阅读。并学习使用:所有类和方法都有文档记录。您应该为列表命名cars
,而不是car
,以便其名称与其内容匹配
然后你可以简单地说cars.get(0).getPrice()
。如果你的Car
类还没有这个方法,你需要创建它。如果你想得到所有汽车的价格,你必须遍历数组列表:
public static void processCars(ArrayList<Cars> cars) {
for (Car c : cars) {
System.out.println (c.getPrice());
}
}
publicstaticvoidprocesscars(arraylistcars){
用于(c车:汽车){
System.out.println(c.getPrice());
}
}
主类
import java.util.*;
导入java.lang.*;
导入java.io.*;
哈里班
{
公共静态void main(字符串[]args)引发异常
{Scanner s=新的扫描仪(System.in);
int i=0;
int b=0;
HashSet h=新的HashSet();
试一试{
对于(i=0;ArrayList中的iType应该是Car,而不是Cars;-)@stiank81:我想你的意思是针对home的答案的评论,不是我的…事实上你的评论也错了,但是Petar编辑了它..不过没什么大不了..嗨,谢谢你的帮助,我试过了,但是它说它需要一个int,但是发现了空值。我不确定如何绕过它。再次感谢这个解决方案完全适合你的问题我希望
Car firstCar = car.get(0);
String price = firstCar.getPrice();
String price = car.get(0).getPrice();
public static void processCars(ArrayList<Cars> cars) {
for (Car c : cars) {
System.out.println (c.getPrice());
}
}
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;
}
}
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());
}
}}