我努力從存儲的計算機模型陣列中打印兩個不同的值。目前我的程序從索引中打印第一臺計算機,但我無法得到如何僅打印特定模型中的一種。 這是我的主類Java如何從一個陣列中打印兩個不同的值
ComputerList list = new ComputerList();
Coputer item;
String model;
switch (option)
{
case 'M':
model = Console.askString("Enter model?: ");
item = list.findModel(model);
if (item == null)
System.out.println("Cannot find " + model);
else
item.print("Computer details..." + model);
的片段......這是我的COMPUTERLIST類
ArrayList<Laptop> laptops;
private ArrayList<String> models;
public SerialList()
{
laptops = new ArrayList<Laptop>();
models = new ArrayList<String>();
}
public void add(Computer anComputer)
{
laptops.add(anComputer);
models.add(anComputer.getModel());
}
public void print()
{
int nItems = computer.size();
for (int i=0; i<nItems; i++)
{
System.out.println(computers.get(i));
}
public Computer findModel(String aModel)
{
int index = models.indexOf(aModel);
if (index == -1)
return null;
else
return computers.get(index);
}
}
我真的努力解決這個問題了幾天,但大多數教程是基於數字,價值觀等。 我將非常感謝這件事的任何幫助。 Regards
您在'print'方法中缺少'for'循環的右括號'}'。 – jbabey 2013-05-09 13:38:12
我真的不明白你想要做什麼,請你更具體一些。 – Genjuro 2013-05-09 13:41:53
我目前的應用程序添加了計算機模型到陣列,但我想增加用戶的可能性,以找到可用的模型,只需通過給模型例如如果用戶選擇Linux然後所有基於Linux系統的計算機將被打印。我仍然不知道如何將此功能添加到我的應用程序。 – piotre 2013-05-09 14:51:53