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;

Recommended Answers

All 3 Replies

Please describe the problem, what is your approach, and what are the results so far.

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

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);
		}
	 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.