Currently trying to understand why this intersection method doesn't work properly, I think the problem is the if statement line but not sure if the problem solely lies there. I've included the necessary files to make my question clearer. The setIntersection method is part of the ArraySet class. The main method is in Hw8p2.

public SetADT<T> setIntersection (SetADT<T> set)
  {
    ArraySet<T> intersection = new ArraySet<T>();
  
    for (int index=0; index < count; index++)
      intersection.add(contents[index]);
	 
      Iterator<T> scan = set.iterator();
	 
      while (scan.hasNext()){
        //if (!(intersection.contains(scan.next())))
        if (intersection.contains(scan.next())==false)
          intersection.remove(scan.next());
      }
    return intersection;

Edited 6 Years Ago by kezkez: n/a

Attachments
import jss2.*;
import java.util.*;


public class Hw8p2 {
	 
	 public static void main (String[] args) { 
	
	 ArraySet<String> one = new ArraySet<String>();
	 ArraySet<String> two = new ArraySet<String>();
	 ArraySet<String> un = new ArraySet<String>();
	 ArraySet<String> diff = new ArraySet<String>();
	 ArraySet<String> inter = new ArraySet<String>();
	 
	 System.out.println("-----------Set One------------");
	 String s_one_1 = "one";
	 String s_one_2 = "two";
	 String s_one_3 = "three";
	 one.add(s_one_1);
	 one.add(s_one_2);
	 one.add(s_one_3);
	 System.out.println(one);
	 System.out.println("-----------Set Two------------");
	 String s_two_1 = "four";
	 String s_two_2 = "five";
	 String s_two_3 = "six";
	 two.add(s_two_1);
	 two.add(s_two_2);
	 two.add(s_two_3);
	 System.out.println(two);
	 System.out.println("-----Union of Sets One and Two (Test)----");
	 un = (ArraySet<String>)one.union(two);
	 System.out.println(un);
	 System.out.println("----------Set Three-----------");
	 ArraySet<String> three = new ArraySet<String>();
	 String s_three_1 = "one";
	 three.add(s_three_1);
	 System.out.println(three);
	 System.out.println("-------Difference Set One minus Set Three------");
	 diff = (ArraySet<String>)one.setDifference(three);
	 System.out.println(diff);
    System.out.println("----------Set Four-----------");
	 ArraySet<String> four = new ArraySet<String>();
	 String s_four_1 = "one";
	 String s_four_2 = "two";
	 String s_four_3 = "three";
	 four.add(s_four_1);
    four.add(s_four_2);
	 four.add(s_four_3);
	 System.out.println(four);
	 System.out.println("-------Intersection------");
	 inter = (ArraySet<String>)one.setIntersection(four);
	 System.out.println(inter);
	 
	 }
}

I am trying to return an intersection of two arrayset strings. Say arrayset one has three strings "one" "two" "three" and the second has "one" "three" "five", the intersection values returned would be "one" "three".

/*******************************************************************
    Takes a set as a parameter and returns a set of all elements of the
	 current set that are also in the parameter set.
  *******************************************************************/
  public SetADT<T> setIntersection (SetADT<T> set)
  {
    ArraySet<T> intersection = new ArraySet<T>();
  
    for (int index=0; index < count; index++)
	   intersection.add(contents[index]);
	 
	 Iterator<T> scan = set.iterator();
	 while (scan.hasNext())
	   if (!(intersection.contains(scan.next())))
        intersection.remove(scan.next());
	 
    return intersection;
  }
}
import jss2.*;
import java.util.*;


public class Hw8p2 {
	 
	 public static void main (String[] args) { 
	
	 ArraySet<String> one = new ArraySet<String>();
	 ArraySet<String> two = new ArraySet<String>();
	 ArraySet<String> un = new ArraySet<String>();
	 ArraySet<String> diff = new ArraySet<String>();
	 ArraySet<String> inter = new ArraySet<String>();
	 
	 System.out.println("-----------Set One------------");
	 String s_one_1 = "one";
	 String s_one_2 = "two";
	 String s_one_3 = "three";
	 one.add(s_one_1);
	 one.add(s_one_2);
	 one.add(s_one_3);
	 System.out.println(one);
          System.out.println("----------Set Four-----------");
	 ArraySet<String> four = new ArraySet<String>();
	 String s_four_1 = "one";
	 String s_four_2 = "three";
	 String s_four_3 = "five";
	 four.add(s_four_1);
          four.add(s_four_2);
	 four.add(s_four_3);
	 System.out.println(four);
	 System.out.println("-------Intersection------");
	 inter = (ArraySet<String>)one.setIntersection(four);
	 System.out.println(inter);

Unfortunetly when I run I am getting
-----------Set One------------
one
two
three

----------Set Four-----------
one
three
five

-------Intersection------
Exception in thread "main" java.util.NoSuchElementException
at jss2.ArrayIterator.next(ArrayIterator.java:42)
at jss2.ArraySet.setIntersection(ArraySet.java:262)
at Hw8p2.main(Hw8p2.java:54)
_______________________________________________
42 throw new NoSuchElementException();
262 intersection.remove(scan.next());
54 inter = (ArraySet<String>)one.setIntersection(four);

Attachments
import jss2.*;
import java.util.*;


public class Hw8p2 {
	 
	 public static void main (String[] args) { 
	
	 ArraySet<String> one = new ArraySet<String>();
	 ArraySet<String> two = new ArraySet<String>();
	 ArraySet<String> un = new ArraySet<String>();
	 ArraySet<String> diff = new ArraySet<String>();
	 ArraySet<String> inter = new ArraySet<String>();
	 
	 System.out.println("-----------Set One------------");
	 String s_one_1 = "one";
	 String s_one_2 = "two";
	 String s_one_3 = "three";
	 one.add(s_one_1);
	 one.add(s_one_2);
	 one.add(s_one_3);
	 System.out.println(one);
	 System.out.println("-----------Set Two------------");
	 String s_two_1 = "four";
	 String s_two_2 = "five";
	 String s_two_3 = "six";
	 two.add(s_two_1);
	 two.add(s_two_2);
	 two.add(s_two_3);
	 System.out.println(two);
	 System.out.println("-----Union of Sets One and Two (Test)----");
	 un = (ArraySet<String>)one.union(two);
	 System.out.println(un);
	 System.out.println("----------Set Three-----------");
	 ArraySet<String> three = new ArraySet<String>();
	 String s_three_1 = "one";
	 three.add(s_three_1);
	 System.out.println(three);
	 System.out.println("-------Difference Set One minus Set Three------");
	 diff = (ArraySet<String>)one.setDifference(three);
	 System.out.println(diff);
    System.out.println("----------Set Four-----------");
	 ArraySet<String> four = new ArraySet<String>();
	 String s_four_1 = "one";
	 String s_four_2 = "three";
	 String s_four_3 = "five";
	 four.add(s_four_1);
    four.add(s_four_2);
	 four.add(s_four_3);
	 System.out.println(four);
	 System.out.println("-------Intersection------");
	 inter = (ArraySet<String>)one.setIntersection(four);
	 System.out.println(inter);
	 
	 }
}
public SetADT<T> setIntersection (SetADT<T> set)
  { 
    //create set to hold intersection
    ArraySet<T> intersection = new ArraySet<T>();
    
	 //go through current set
	 for (int index=0; index<count; index++) {
	   //get obj from current set
		T obj = contents[index];
		//if set contains the obj add it to the intersection set
		if(set.contains(obj)) {
	     intersection.add(obj);
		}
	 }
This question has already been answered. Start a new discussion instead.