我一直在嘗試將一個較舊的賦值從一個數組切換到arraylist
,但無論出於何種原因,當我修改它以使用arrayList
時,我的查找方法不起作用。似乎總是返回-1
。ArrayList與數組。爲什麼一個人在工作,一個人不在?
這是一大類的一部分,所以我不希望包括,除非完全必要的一切,但我沒有包括在案件的聲明,他們是很重要的:
public class Switch {
private SwitchEntry[] camTable;
private int numEntries;
private int maxEntries;
public Switch() {
camTable = new SwitchEntry[100]; // default value
numEntries = 0;
maxEntries = 100;
}
public Switch(int maxEntries) {
camTable = new SwitchEntry[maxEntries];
numEntries = 0;
this.maxEntries = maxEntries;
}
原文:
public int find (MACAddress source) {
int found = -1;
for (int i=0; i < numEntries; i++)
if (source.isEqual (camTable[i].getAddress())){
found = i;
break;
}
return found;
}
修改:
public int find (MACAddress source) {
int found = -1;
for (int i=0; i < numEntries; i++)
if (source.isEqual (camTable.get(i).getAddress())){
found = i;
break;
}
return found;
}
凡numEntries被修改&在新條目添加到該ArrayList:
public void processFrame(Frame inFrame) {
// first, add source MAC to camTable if not already there
if (find(inFrame.getSource()) == -1) {
if (numEntries >= maxEntries) {
System.out.println ("Error...camTable is full - cannot add " + inFrame.getSource());
} else {
camTable.add(new SwitchEntry(inFrame.getSource(), inFrame.getPort())); //PROBLEM LINE
System.out.println ("Adding " + inFrame.getSource() + " to camTable");
}
}
//process frame
int index = find(inFrame.getDestination());
if (index != -1){
System.out.print ("Sending frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
System.out.println (" out port " + camTable.get(index).getPort());
} else {
System.out.print ("Flooding frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
System.out.println (" out all ports" );
}
}
解決方案:
camTable.add(numEntries++, new SwitchEntry(inFrame.getSource(),inFrame.getPort()));
where is arraylist? –
@VipinJain camTable是arralist – SpringLearner
@VipinJain在他的camTable.get(i)中......我相信。你如何獲得'numEntries' Sentience?這可能是問題所在。什麼是isEqual()方法? –