11 Replies - 19027 Views - Last Post: 27 September 2009 - 03:32 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

union and intersection of two ArrayLists

Posted 27 September 2009 - 12:18 PM

this is what 2 methods i am working on...

public IntSet union (IntSet otherSet)
returns a set containing one and only one copy of every element that is in either this or otherSet.

public IntSet intersection (IntSet otherSet)
returns a set containing one and only one copy of every element that is in both this and otherSet.



here is bits and pieces of my code that apply to the problems at hand. there is 3 errors that i can see and i commented in the code where they are and what the error is.
import java.util.*;

public class IntSet
{
	private ArrayList<Integer> list;
	
	public IntSet()
	{
		list = new ArrayList<Integer>();
	}
 
......


	public IntSet union(IntSet otherSet)
	{
		ArrayList unionArray = new ArrayList<Integer>();
		for(Integer i : list)
		{
			if(unionArray.contains(i) = false)   //error says "unexpected type"
			{
				unionArray.add(i);
			}
		}
		for(Integer j : otherSet.list)
		{
			if(unionArray.contains(j) = false)	//error says "unexpected type:
			{
				unionArray.add(j);
			}
		}
		return unionArray;
	}
	
	public IntSet intersection(IntSet otherSet)
	{
		ArrayList intersectionArray = new ArrayList<Integer>();
		for(Integer i : list)
		{
			intersectionArray.add(i);
		}
		for(Integer j : otherSet.list)
		{
			intersectionArray.add(j);
		}
		return intersectionArray;	 //incompatible types - found java.util.ArrayList but expected IntSet
	}



Is This A Good Question/Topic? 0
  • +

Replies To: union and intersection of two ArrayLists

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:21 PM

 if(unionArray.contains(i) == false)



use equality "==" when comparing booleans

edit: grrr damn typos

This post has been edited by mostyfriedman: 27 September 2009 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:23 PM

The first 2 errors are because you're trying to use the = operator...you want ==. Or you can use

if (!unionArray.contains(i))


The second one is because you're trying to return an ArrayList, when you told the compiler to expect to return an IntSet.

This post has been edited by Locke: 27 September 2009 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:26 PM

public ArrayList<Integer> intersection(IntSet otherSet) {
Was This Post Helpful? 0
  • +
  • -

#5 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:37 PM

thanks for the first two errors, i knew it had to be something simple that was wrong with it.

now both methods show the same error on the 'return' lines, which is "incompatible types - found java.util.ArrayList but expected IntSet". pbl, you suggested that i change my header of the methods but the professor gives the headers to the methods and we have to fill in the rest so i can't change them. any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:40 PM

Like I said, you're returning an ArrayList. You want to return an IntSet. You'll need to construct an IntSet object using the new ArrayList as the instance variable for that object.
Was This Post Helpful? 0
  • +
  • -

#7 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:45 PM

sorry i am still a novice at this. can you start me off on some code cause i'm not sure how to do what you said.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 12:53 PM

You already have this constructor for your InSet
	public IntSet()
	{
		list = new ArrayList<Integer>();
	}




You will need another one that receives the ArrayList as parameter
	public IntSet(ArrayList<Integer> list)
	{
		this.list = list;
	}




At the end of your method you will have to return a new InSet object build with the arraylist you just built

	public IntSet union(IntSet otherSet)
	{
		ArrayList unionArray = new ArrayList<Integer>();
		for(Integer i : list)
		{
			if(unionArray.contains(i) = false)   //error says "unexpected type"
			{
				unionArray.add(i);
			}
		}
		for(Integer j : otherSet.list)
		{
			if(unionArray.contains(j) = false)	//error says "unexpected type:
			{
				unionArray.add(j);
			}
		}
		return new InSet(unionArray);
	}


Was This Post Helpful? 0
  • +
  • -

#9 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 01:41 PM

pbl i just put in what you had and when i compile with that a window comes up that has a header of "Compiler Warnings" and says in the window "Warnings from last compilation" and "Note: C:/Users/Scoott Lyons/... uses unchecked or unsafe operations." and then "Note: Recompile with -Xlint:unchecked for details."

it says at the bottom that class was compiled with no syntax errors tho. what does that mean? am i ok to leave it?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 03:04 PM

View Postmattlyons, on 27 Sep, 2009 - 12:41 PM, said:

pbl i just put in what you had and when i compile with that a window comes up that has a header of "Compiler Warnings" and says in the window "Warnings from last compilation" and "Note: C:/Users/Scoott Lyons/... uses unchecked or unsafe operations." and then "Note: Recompile with -Xlint:unchecked for details."

it says at the bottom that class was compiled with no syntax errors tho. what does that mean? am i ok to leave it?

We just forgot a <type> arrayList here
replace
ArrayList unionArray = new ArrayList<Integer>();
by
ArrayList<Integer> unionArray = new ArrayList<Integer>();
Was This Post Helpful? 1
  • +
  • -

#11 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 03:25 PM

pbl, thanks, once again.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: union and intersection of two ArrayLists

Posted 27 September 2009 - 03:32 PM

View Postmattlyons, on 27 Sep, 2009 - 02:25 PM, said:

pbl, thanks, once again.

No problem
Just hope you understood why

The constructor of InSet expects an ArrayList<String> and we were feeding it with a simple ArrayList
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1