Buliding English Dictionary Using Hashmap!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    Originally posted by Broke
    Can anyone help me i wanna compare between two ArrayLists if there is a factor element between them the third list would display the elements of both ArrayLists, without duplication. The code i wrote doesn't have syntax error but i think it has logical error and this is my code:-

    public ArrayList checkSym(ArrayL ist art1, ArrayList art2) {

    Iterator it1 = art1.iterator() ;
    Iterator it2 = art2.iterator() ;

    String s = "";
    while (it1.hasNext()) {
    s = (String)it1.nex t();
    while (it2.hasNext()) {
    if (s.equals((Stri ng)it2.next())) {
    art1.remove(s);
    }
    }

    }
    art2.addAll(art 1);
    return art2;
    }
    A lazy way of doing it is to empty all elements into a set which removes the duplicates.
    Another way is to add all element of list1 to list2 if they are not already there. You would use the contains method this way

    Code:
     for(int i = 0;i < list1.size();i++) { 
      if(!list2.contains(list1.get(i))) {
    	list2.add(list1.get(i));
      }
      else {
      //already there so do nothing
      } 
    }
    well something like that.

    Comment

    Working...