【发布时间】:2021-02-16 19:56:48
【问题描述】:
假设我有一个文件,其中包含多个平面和有关平面结构的信息:
type,max speed,name,color
commercial plane,750,boeing_777,white
private jet,800,cynthia,blue
propeller plane,200,venus,yellow
commercial plane,640,boeing_737,white
commercial plane,550,airbus,blue
propeller planes,150,tatoo,yellow
private jet,670,VLJ, white
由此我创建了 Planes 类
public Planes{
static int counter = 1;
String type;
int maxSpeed;
String name;
String color;
int id;
public Planes(List<String> element) {
this.type = element.get(0)
this.maxSpeed = element.get(1)
this.name = element.get(2)
this.color = element.get(3)
this.id = counter;
counter++;
}
我做了几个方法,现在我的 Planes 类型的对象在一个 ArrayList 中 我的目标是输出一个字符串,用于打印类型和平面类型具有的不同颜色的数量。所以输出将是:
commercial plane,2 //2 because it has both white and blue NOT 3
private jet,2
propeller plane,1
我尝试了以下方法:
for (int i = 0; i < arrayListPlanes.size(); ++i) {
int it = 0;
newList.add(arrayListPlanes.get(i).getColor());
if(newList.contains(arrayListPlanes.get(i).getColor())) {
it = it + 1;
}
String line += arrayListPlanes.get(i).type() + ", " +
Collections.frequency(newList, arrayListPlanes.get(i).getColor()) +
"\n";
}
实际列表要长得多,并且包含多种其他类型的飞机,因此我无法为每种类型的飞机制作一个对象。
我为写得不好的标题道歉
【问题讨论】: