java - set.iterator().......where am i wrong? -


trying implement set interface , use iterator union method. inside union method never enters while loop. not add elements parameter "set" union method has. ideas? :)

package linkedset;

import java.util.iterator; import java.util.nosuchelementexception;

import linkedlist.linearnode;

public class linkedset implements setadt {

private int size; private linearnode<t> front; private boolean allownullelement = false;  // ///////////////////////////////////////////////////// private class linkedsetiterator implements iterator<t> {      private linearnode<t> currentnode;      public linkedsetiterator() {         currentnode = front;     }      @override     public boolean hasnext() {         return currentnode == null;     }      @override     public t next() {         if (!hasnext()) {             throw new nosuchelementexception();         }         t temp = currentnode.getelement();         currentnode = currentnode.getnext();         return temp;     } } /////////////////////////////////////////////////// @override public iterator<t> iterator() {     return new linkedsetiterator();  } public linkedset(boolean allownullelement) {     size = 0;     front = new linearnode<t>();     this.allownullelement = allownullelement; }  public string tostring() {      string str = "{";     (int = 0; < size; i++) {         str += getnode(i).getelement();         if (i < size - 1) {             str += (", ");         }     }     return str + "}";  }  private linearnode<t> getnode(int index) {     linearnode<t> current = front;     (int = 0; < index; i++) {         current = current.getnext();     }     return current; }  @override public void add(t element) {     if (element == null && !allownullelement) {         throw new illegalargumentexception(                 "null element not allowed add");     }     front = new linearnode<t>(element, front);     size++;  }  @override public t remove(t element) {      if (!(contains(element))) {         throw new illegalstateexception();     }     t temp = null;     ;     (int = 0; < size; i++) {          temp = getnode(i).getelement();         if (temp.equals(element) || temp == element) {             if (i == 0) {                 front = front.getnext();                 break;              } else {                 getnode(i).setelement(front.getelement());                 front = front.getnext();                 break;             }         }     }     size--;     return temp; }  @override public boolean contains(t element) {     (int = 0; < size; i++) {         if (getnode(i).getelement() != null                 && getnode(i).getelement().equals(element)) {             return true;         }         if (getnode(i).getelement() == null                 && getnode(i).getelement() == element) {             return true;         }     }     return false; }  @override public boolean isempty() {      return size == 0; }  @override public int size() {     return size; }  @override public boolean issubset(setadt<t> set) {     if (set == null) {         throw new illegalargumentexception();     }      (int = 0; < size; i++) {         t temp = getnode(i).getelement();         if (!(set.contains(temp))) {             return false;         }     }     return true; }  @override public setadt<t> intersection(setadt<t> set) {     if (set == null) {         throw new illegalargumentexception();     }     linkedset<t> temp = new linkedset<t>(allownullelement);      (int = 0; < size; i++) {          if (set.contains(getnode(i).getelement())) {             temp.add(getnode(i).getelement());         }     }     return temp; }  public setadt<t> union(setadt<t> set) {      if (set == null) {         throw new illegalargumentexception();     }      linkedset<t> temp = new linkedset<t>(allownullelement);     linearnode<t> node = front;      (int = 0; < size; i++) {         t el = node.getelement();         temp.add(el);         node = node.getnext();     }      iterator<t> iterator = set.iterator();     while (iterator.hasnext()) {         temp.add(iterator.next());      }     return temp; } 

}

the linkedsetiterator.hasnext() method return false if set nonempty. corrected inverting condition:

@override public boolean hasnext() {     return currentnode != null; } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -