是否有任何替代使用散列表? 我有這個代碼,並想知道如果我可以做,而不使用它。我的工作是找到,但我被告知不要被某些人使用。任何幫助表示讚賞。散列表替代
import java.util.Hashtable;
import java.util.Enumeration;
public class Cart
{
public Hashtable items = new Hashtable();
public Enumeration getEnumeration()
{
return items.elements();
}
public void addItem(String itemId,String desc, float price, int quantity)
{
String[] item = {itemId, desc, Float.toString(price),
Integer.toString(quantity)};
if (items.containsKey(itemId))
{
String[] tmpItem = (String[])items.get(itemId);
int tmpQuant = Integer.parseInt(tmpItem[3]);
quantity += tmpQuant;
tmpItem[3] = Integer.toString(quantity);
}
else {
items.put(itemId, item);
}
}
public float getTotalCost() {
Enumeration e = items.elements();
String[] tmpItem;
float totalCost = 0.00f;
while (e.hasMoreElements()) {
tmpItem = (String[])e.nextElement();
totalCost += (Integer.parseInt(tmpItem[3]) *
Float.parseFloat(tmpItem[2]));
}
return totalCost;
}
}
應該cuncurrently運行代碼?即來自多個線程? – einpoklum