This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to remove  object of perticular class from ArrayList..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to remove  object of perticular class from ArrayList..?" Watch "how to remove  object of perticular class from ArrayList..?" New topic
Author

how to remove object of perticular class from ArrayList..?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all,
I have Three classes p1,p2,p3 and i had object for that perticular class's

I had arraylist in which it stores all the objects of three different classes and i want to remove the objects of p3 in that arraylist with in one statement .and i had one clue that removeAll() method in collection which takes collection as its parameter.please give me a hand for this problem

Creativity is nothing but Breaking Rules
Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64
santhosh.R gowda wrote:Dear all,
I have Three classes p1,p2,p3 and i had object for that perticular class's

I had arraylist in which it stores all the objects of three different classes and i want to remove the objects of p3 in that arraylist with in one statement .and i had one clue that removeAll() method in collection which takes collection as its parameter.please give me a hand for this problem


you can use remove method
like


santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
but we have to check it through the loop i want to remove at once
Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64
santhosh.R gowda wrote:but we have to check it through the loop i want to remove at once

use


and if match found break the loop

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
and if match found break the loop

Please understand the problem correctly i must not use loop ok
Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64
santhosh.R gowda wrote:
and if match found break the loop

Please understand the problem correctly i must not use loop ok

Dear Gowda,

My first post was without loop.
Its not matter you are using loop or without loop.

Please not confused.
for example..
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear Ranveer K Kumar

You are removing only one object of perticular class see if you are having so many object of perticular class if i remove one object it should remove all the objects of that class from list
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Check out ArrayList#removeAll(Collection<?> c)

Also, please read http://faq.javaranch.com/java/ShowSomeEffort
If you had checked the API after being told of the remove method, I am sure you would have found the removeAll yourself.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Example let P,Q and S are the classes
p1,p2,p3 are the objects of P
q1,q2,q3 are the objects of Q
s1,s2,s3 are the objects of S

List list = new ArrayList();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(q1);
list.add(q2);
list.add(q3);
list.add(s1);
list.add(s2);
list.add(s3);

please remove 'S' objects in that list with out iterating it dont use getClass() and getInstance() method
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Pseudo code:
1) Create a sublist for all 'S' Objects.
2) Use the removeAll method on the main list using this sublist.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
No sir actually i had gone for interview they asked me to not sublist and they had given me the clue that use removeAll method thats all
Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64

This case best way to use generic to add the object in the list.
In above example if you are using n no. of objects then how will you remove without iterating. the best way to use is loop..
If you are able to remove then use
list.remove(int)
if you know the best way then let me know...

Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64
this is tricky Q and atleast my knowledge removeAll will remove all the particular Collection(object) from list, and sublist not to be used so
collection cannot be passed.
I am agree with Maneesh, but also sublist not to be used, loop is already not to used..
then only way that remove mannually one by one...

But the main thing I am not sure about the other methods to solve this problem...Please let me know
if any other solution availabe..
Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


Or even better answer :
This code is wrong because the next letter after Q is R


[My Blog]
All roads lead to JavaRanch
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

It's not the answer, is it ? Because singleList doesn't accomplish what you are trying to do.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to remove object of perticular class from ArrayList..?