java - ArrayList 与数组。为什么一个工作,一个不工作?

标签 java arrays arraylist

我一直在尝试将旧的分配从数组切换到 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;
}

原文:

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()));

最佳答案

试试这个

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 的对象。在 if 条件下,我检查 source.getAddress 到 tempAdd.getAddress() 是否相同,然后它将重新调整 ArrayList 的索引。这里 ArrayList局部变量 但你可以创建一个 类变量

关于java - ArrayList 与数组。为什么一个工作,一个不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35715538/

相关文章:

java - 格里芬-JavaFX : View throws NPE concerning the model

java - 未指定“url”属性,无法配置嵌入数据源

c - 值不是数组、指针或 vector

java - 如何删除arraylist重复值

java - 通过单击 JButton 绘制形状

java - 如何将图像存储到 Oracle 数据库?

java - 如何将字节[]存储到android/java中的单个变量中

java - 如何遍历数组并找到最大值 : Java

java - (Java) 删除 ArrayList 元素会在内存中腾出更多空间吗?

java - 如何在 Java 中安全地访问数组线程?