ArrayList与Array。为什么一个人工作而另一个人不工作?

时间:2016-03-01 05:45:09

标签: java arrays arraylist

我一直在尝试将较旧的作业从数组切换为arraylist,但无论出于何种原因,当我修改它以使用arrayList时,我的find方法无效。似乎总是返回-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;
}

原件:

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的位置&amp;将新条目添加到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()));

3 个答案:

答案 0 :(得分:2)

试试这个

public int find (MACAddress source) {
    int found = -1;
    ArrayList<MACAddress> camTable = new ArrayList<MACAddress>();
    ListIterator<MACAddress> itr = camTable.listIterator();
    while(itr.hasNext()){
        MACAddress tempAdd = itr.next();
        if(source.getAddress().equals(tempAdd.getAddress())){
            found = itr.nextIndex();
            return found;
        }
        return found;           
    }

我假设在ArrayList中存储MACAddress的对象。如果条件我检查source.getAddress到tempAdd.getAddress()是相同的,那么它将重新调整ArrayList的索引。此处ArrayListlocal variable,但您可以创建为class variable

答案 1 :(得分:2)

使用Contain方法收集。(ArrayList)

http://www.tutorialspoint.com/java/util/arraylist_contains.htm

答案 2 :(得分:1)

解决方案很直接,只是我的疏忽。我所要做的就是将numEntries添加回我的add语句中,从数组更改为arrayList后我忽略了修复

解决方案现已发布在原始问题中: