Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to subtract two arrayLists?

 
majid nakit
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have two ArrayList A and B

how to do : A - B
in other word take off all the objects in B from A.

Thanks
[ October 27, 2008: Message edited by: Bear Bibeault ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a.removeAll(b);
 
Jackie Gleason
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know you would have to iterate through set a and remove all instances of set b
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
a.add("Leaders");
List<String> b = new ArrayList<String>();
b.add("Goodbye");
b.add("World");
b.add("Leaders");
for(String bs:b){
a.remove(bs);
}
for(String as:a){
println(as);
}

This would only print out Hello
[ October 27, 2008: Message edited by: Jackie Gleason ]
 
Jackie Gleason
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
a.removeAll(b);


As far as I know the function he gives you here does not exist(at least not according to the Java API) There is a removeAll function in the List class but it removes all the items in the list

void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this list.
void removeAll()
Removes all items from this list.
void removeItemListener(ItemListener l)
Removes the specified item listener so that it no longer receives item events from this list.

Update: Ok so now I am baffled I tried this and it DID work. I am not sure why considering according to the 1.6 doc there is not a function. Anyone have an explination for this? Am I looking at an old API or something?

http://java.sun.com/javase/6/docs/api/
[ October 27, 2008: Message edited by: Jackie Gleason ]
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a removeAll method, but you will only find it if you look in List, rather than List.








There are two classes/interfaces called List!
 
Jackie Gleason
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhhh oops haha
 
majid nakit
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I did but I have an IndexOutOfBoundsException :

public ArrayList substacrtArrays(ArrayList A, ArrayList B)
{
int sizeAbefore = A.size();
try {
for(int k=0; k < B.size();k++)

{
for(int i=0;i<A.size();i++)
{
Role roleA = (Role)A.get(k);
Role roleB = (Role)B.get(i);
if((roleA.getValue()).equals(roleB.getValue()))
A.remove(roleA);

}
}
int sizeAafter = A.size();
}catch(Exception e){
System.out.println(e);
}

return A;


}

your help is appreciated.
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to go through those for loops carefully counting how many elements you have. Imagine list 1 contains a b c d e and list 2 contains b d f h. Go through them with pencil and paper and see how many iterations you get, and whether you go beyond the size of either list.

In your code, you have two classcasts which should be unnecessary; you should override the equals() method in your Role class, and use equals to check whether the two values are "the same." Then you don't need your casts.
You also have sizeAbefore and sizeAafter local variables which you don't appear to use; you should delete them both.
It is probably better to use an Iterator and its remove method to go through the lists.
Also check carefully which list you are getting the i and k variables from.
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please use the code button and correct indentation; your code is difficult to read.
 
Vlad Jigounov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
list1.removeAll(list2); will do the job, but it cost you N1*N2 operations (very slow).
If you work with big arrays you can improve performance like this:


 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Agree that using Sets will improve performance for that sort of thing.

Please use the code button; sinc eyou are new I have edited your post and you can see how much better it looks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic