java - ArrayList vs. Array. Why is one working and one isn't? -


i've been trying switch older assignment on array arraylist, whatever reason find method not working when modify use arraylist.. seems returning -1.

this part of large class don't want include unless necessary, did include declarations in case important:

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; } 

original:

public int find (macaddress source) {     int found = -1;     (int i=0; < numentries; i++)         if (source.isequal (camtable[i].getaddress())){             found = i;             break;         }     return found;                } 

modified:

public int find (macaddress source) {     int found = -1;     (int i=0; < numentries; i++)         if (source.isequal (camtable.get(i).getaddress())){             found = i;             break;         }     return found;                } 

where numentries modified & new entries added arraylist:

public void processframe(frame inframe) {     // first, add source mac camtable if not there     if (find(inframe.getsource()) == -1) {         if (numentries >= maxentries) {             system.out.println ("error...camtable full - cannot add " + inframe.getsource());             } else {              camtable.add(new switchentry(inframe.getsource(), inframe.getport())); //problem line             system.out.println ("adding " + inframe.getsource() + " camtable");         }     }      //process frame     int index = find(inframe.getdestination());     if (index != -1){         system.out.print ("sending frame data " + inframe.getdata() + " " + inframe.getsource() + " " + inframe.getdestination());         system.out.println (" out port " + camtable.get(index).getport() );     } else {         system.out.print ("flooding frame data " + inframe.getdata() + " " + inframe.getsource() + " " + inframe.getdestination());         system.out.println (" out ports"  );      }  } 

solution:

camtable.add(numentries++, new switchentry(inframe.getsource(),inframe.getport())); 

try this

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;                } 

i assume in arraylist store objects of macaddress. in if condition check source.getaddress tempadd.getaddress() same retun index of arraylist. here arraylist local variable can create class variable


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -