2016-03-01 26 views
1

我一直在嘗試將一個較舊的賦值從一個數組切換到arraylist,但無論出於何種原因,當我修改它以使用arrayList時,我的查找方法不起作用。似乎總是返回-1ArrayList與數組。爲什麼一個人在工作,一個人不在?

這是一大類的一部分,所以我不希望包括,除非完全必要的一切,但我沒有包括在案件的聲明,他們是很重要的:

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

where is arraylist? –

+0

@VipinJain camTable是arralist – SpringLearner

+0

@VipinJain在他的camTable.get(i)中......我相信。你如何獲得'numEntries' Sentience?這可能是問題所在。什麼是isEqual()方法? –

回答

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的對象。在if條件中,我檢查source.getAddress爲tempAdd.getAddress()是否相同,那麼它將重新調用ArrayList的索引。這裏ArrayListlocal variable,但你可以創建爲class variable

+0

我欣賞你的幫助,但我只是通過在我的camTable.add中添加numEntries ++來解決問題。如果我有15的聲望,你會看到upvote ...不幸的是我沒有。 :( – Sentience

+0

foreach循環很多,比迭代器更清晰 – tucuxi

1

解決方案很簡單,只是我的疏忽。我所要做的就是添加numEntries回到我的附加聲明,我忽略了從一個數組轉換後固定到ArrayList中

的解決辦法是張貼在現在原來的問題:

相關問題