我是新來的Java,我有下面的代碼(v是一個向量) ,我不明白這一點:Java - 「(Customer)v.get(i)」做什麼?
(客戶)v.get(I)
請給我解釋一下.tks
public void insert(Customer c) {
boolean checkExist = true;
if (checkExist && !isIn(c,v)) {
for (int i = 0; i < v.size(); i++) {
int check = c.compareTo((Customer) v.get(i));
if (check < 0) {
Customer x = new Customer();
x = (Customer) v.get(i);
v.set(i, c);
c = x;
}
}
v.add(c);
}
}
public boolean isIn(Customer c, Vector els) {
c = new Customer();
for (int i = 0; i < els.size(); i++) {
if (c.equals(els.get(i))) {
return true;
}
}
return false;
}
你幾乎肯定不想'c = new Customer()'行。它會覆蓋作爲參數傳入的值;你可能根本不會傳遞'客戶c'。 –
http://stackoverflow.com/questions/5289393/casting-variables-in-java –
http://stackoverflow.com/questions/5306835/casting-objects-in-java –