Я пытался переключить более старое назначение с массива на ArrayList , но по какой -то причине мой метод поиска не работает, когда я изменяю его для использования ArrayList .., кажется, всегда возвращается -1 .
Это часть большого класса, поэтому я не хочу все это.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;
}
< /code>
Original: < /p>
public int find (MACAddress source) {
int found = -1;
for (int i=0; i < numEntries; i++)
if (source.isEqual (camTable.getAddress())){
found = i;
break;
}
return found;
}
< /code>
Модифицирован: < /p>
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;
}
< /code>
Где Numentries модифицируется и где новые записи добавляются в ArrayList: < /p>
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" );
}
}
Подробнее здесь: https://stackoverflow.com/questions/357 ... d-one-isnt