Java 如何打印结果?
嗨,我有这样的东西:Java 如何打印结果?,java,Java,嗨,我有这样的东西: case 9: vehicleList.sort(Comparator.comparingInt(Vehicle::getMileage)); // System.out.println(here); break; System.out.println(vehicleList); 如何打印排序结果?vehicleList.forEachv->System.out.prin
case 9:
vehicleList.sort(Comparator.comparingInt(Vehicle::getMileage));
// System.out.println(here);
break;
System.out.println(vehicleList);
如何打印排序结果?vehicleList.forEachv->System.out.printlnv.GetMiledge;
根据您希望它的输出方式,您可以这样输出整个列表:
case 9:
vehicleList.sort(Comparator.comparingInt(Vehicle::getMileage));
// System.out.println(here);
break;
System.out.println(vehicleList);
这将在vehicleList的每个元素上调用toString并在一行中显示它。如果希望每个条目都位于自己的行中,可以将其放入循环中:
for(Vehicle vehicle : vehicleList)
{
System.out.println(vehicle);
}
如Andreas所述,为了避免在输出车辆时获得无意义的字符串,请确保在返回字符串的类中实现toString方法。System.out.printlnvehicleList;因为简单地打印列表似乎非常明显,也许您也需要: