Java 打印数组列表编号
Java print arraylist numbered
我有这个
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));
}
您只需在 return 语句中添加 \r \n
:
public String toString()
{
return "a " + year + " " + make + " " + model +
" with a VIN# of " + vin + " and a mileage of " + miles + "\r \n";
}
我有这个
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));
}
您只需在 return 语句中添加 \r \n
:
public String toString()
{
return "a " + year + " " + make + " " + model +
" with a VIN# of " + vin + " and a mileage of " + miles + "\r \n";
}