我有这个
public String toString()
{
return "a " + year + " " + make + " " + model +
" with a VIN# of " + vin + " and a mileage of " + miles;
}
还有这个:
ArrayList<Auto> autos = new ArrayList<Auto>();
还有这个:
public static void loadNewData(ArrayList<Auto> a, ArrayList<Customer> c)
{
a.add(new Auto(2009,"Ford" , "Mustang","ABC123", 1256.54));
a.add(new Auto(2010,"Chevy","Camero","QWI459", 33.98));
a.add(new Auto(1970,"Pink","Cadillac","950AKH", 212874.51));
a.add(new Auto(2007,"Lotus","Elise MkII","1A2D3F", 12859.90));
c.add(new Customer( "Brett Farve",false));
c.add(new Customer( "Bruce Springsteen",true));
c.add(new Customer( "Mickey Mouse", true));
c.add(new Customer( "Peyton Manning", true));
c.add(new Customer( "Donald Duck", true));
}
然后我加入所有这些并打印:
System.out.println(autos.toString());
但结果是这样的:
[a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54, a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98, a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51, a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9]
我怎样才能打印出来像这样:
- a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
- a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
- a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
- a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
最佳答案
循环列表并为每个条目执行一个System.out.println
。像这样的事情:
for (int i = 0; i < autos.size(); i++) {
System.out.println((i + 1) + ". " + autos.get(i));
}
关于Java打印数组列表编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29177375/