comparing ArrayList's

+ another problem

Page 1 of 1

10 Replies - 645 Views - Last Post: 27 September 2009 - 10:00 AM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

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

comparing ArrayList's

Posted 26 September 2009 - 08:26 PM

Below is what I have written so far and now the next instruction in the assignment is this:

ADD THIS METHOD:
public boolean equals (IntSet otherSet)
-returns true or false as this set and otherSet contain the same elements or not

Can someone explain to me what to do here? I don't even know where to start. Professor says I should pass IntSet, but shouldn't it be the ArrayList I created instead? Someone help me please! :blink:

Here is what I have so far:

import java.util.*;

public class IntSet
{
	private ArrayList<Integer> list;
	
	public IntSet()
	{
		list = new ArrayList<Integer>();
	}
	
	public boolean in(int e)
	{
		int i = list.indexOf(e);
		if(i >= 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public void insert(int e)
	{
		list.add(e);
	}
	
	public void remove(int e)
	{
		list.remove(e);
	}

	public IntSet compare (IntSet otherSet)
	{

	}
}


This post has been edited by mattlyons: 26 September 2009 - 08:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: comparing ArrayList's

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: comparing ArrayList's

Posted 26 September 2009 - 08:32 PM

well you could iterate through one ArrayList and on each iteration check whether the other one contains the current element, once you find an element that is not in the other one, return false
Was This Post Helpful? 0
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: comparing ArrayList's

Posted 26 September 2009 - 08:52 PM

I'm sorry, I put in the wrong this for the last part. It should be:

public boolean equals (IntSet otherSet)



Same question goes with it still. Shouldn't I be passing the ArrayList i created earlier instead of IntSet? I am confused with this part and don't know where to start.

This post has been edited by mattlyons: 26 September 2009 - 08:57 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: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: comparing ArrayList's

Posted 26 September 2009 - 08:58 PM

View Postmattlyons, on 26 Sep, 2009 - 07:52 PM, said:

I'm sorry, I put in the wrong this for the last part. It should be:

public boolean equals (IntSet otherSet)



Same question goes with it still.


public boolean equals (IntSet otherSet) {
   for(Integer i : list) {
	   for(Integer j : otherSet.list) {
		  if(i.equals(j))
			 return true;
	   }
	}
	return false;
}


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: comparing ArrayList's

Posted 26 September 2009 - 09:07 PM

first, thanks for the quick answer.

but i just entered {3, 4, 5, 6} for one and {3, 4} for the other and it returned true. something isn't quite right.
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: comparing ArrayList's

Posted 26 September 2009 - 09:13 PM

or you could just use the predefined equals method, but it will only output true if both ArrayLists are of exactly equal length and contain the same elements in the same order.

or you could use pbl's code but change a lil something, probably slipped him (happens to the best of us)
public boolean equals (IntSet otherSet) {
   for(Integer i : list) {
	   for(Integer j : otherSet.list) {
		  if(!i.equals(j))
			 return false;
	   }
	}
	return true;
}



this should work now

This post has been edited by mostyfriedman: 26 September 2009 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: comparing ArrayList's

Posted 26 September 2009 - 09:39 PM

View Postmattlyons, on 26 Sep, 2009 - 08:07 PM, said:

first, thanks for the quick answer.

but i just entered {3, 4, 5, 6} for one and {3, 4} for the other and it returned true. something isn't quite right.


Quote

public boolean equals (IntSet otherSet)
-returns true or false as this set and otherSet contain the same elements or not


Question was not clear
OK you want to know if both ArrayList contain the same elements (all of them) not just one

No choice you will have to extract as array all the objects from both of them
Sort them and then compare the two sorted arrays
public boolean equals (IntSet otherSet) {
   // first test if same size
   if(list.size() != otherSet.list.size())
	 return false;
   // extract arrays
   Integer[] x = list.toArray();
   Integer[] y = otherSet.list.toArray();
   // sort them
   Arrays.sort(x);
   Arrays.sort(y);
   // compare
   for(int i = 0; i < x.length; i++) {
	 if(x[i].equals(y[i])
	   continue;
	 return false;
   }
   return true;
}


Was This Post Helpful? 0
  • +
  • -

#8 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: comparing ArrayList's

Posted 26 September 2009 - 11:15 PM

thanks guys. its working smoothly now. and pbl, you are right, that instruction the professor gave was pretty unclear but thats not uncommon with this professor. i got 2 or 3 more methods to add by tomorrow night so i might be coming back here later tomorrow.
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: comparing ArrayList's

Posted 26 September 2009 - 11:25 PM

oh man, i just reread the instructions laid out for me and i saw this: "You may not use any other classes from the Java libraries. Use == and != to test ints for equality and inequality." Referring to mostyfriedman's code, does this mean instead of using this line of code here...

if(!i.equals(j))


i can use this line instead...

if(i != j)

Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: comparing ArrayList's

Posted 26 September 2009 - 11:42 PM

you will have to use the equals method since you are comparing Integer objects

This post has been edited by mostyfriedman: 26 September 2009 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: comparing ArrayList's

Posted 27 September 2009 - 10:00 AM

View Postmostyfriedman, on 26 Sep, 2009 - 10:42 PM, said:

you will have to use the equals method since you are comparing Integer objects

Actually you can use a turn around that

for(int i = 0; i < x.lenght; i++) {
   int xx = x[i].intValue();
   int yy = y[i].intValue();
   if(xx != yy)
	  return false;
}
return true;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1