• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Iterator add and it.remove();

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I was just wondering if some one can point me in the right direction.

I a'm, Iterator it over a collection and filtering out records, this is fine.

The problem
I am trying to add and store those records to another collection

This works
remove them from the first collection and

This returns what has been added and removed
return the new collection

This is my class and constructor

public class Club
{
// Define any necessary fields here ...

private List<Membership> memberships;
private List<Membership> members;

/**
* Constructor for objects of class Club
*/
public Club()
{
// Initialise any fields here ...

memberships = new ArrayList<Membership>();
members = new ArrayList<Membership>();
}

and method

public ArrayList <Membership> purge(int month, int year)
{
ArrayList <Membership> members = new ArrayList<Membership>();
if ((month > 0) && (month < 13)) {

Iterator it = memberships.iterator();
while(it.hasNext()) {
Membership monthMem = (Membership) it.next();
if(month == monthMem.getMonth()){
members.add(monthMem);
it.remove();
}
}
}
return members;
}


Any help would be much Appreciatived

Thanks
Mike
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you just described what works, not where the problem lies (Exceptions ? unexpected result ?), how you call the code and so on.

Perhaps the problem has anything to do with the local variable "members" which also exists as a property of the Club class ? And since you pass in month and year in the purge() method i assume you want to check for both


??

also you should use code tags around any code you post. This will make it more readable.
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for looking in on me pascal. There were no errors it just was not behaving the way it was Intended. What do you mean by you should use code tags around any code you post.

You have helped me fix one of my problems by pointing out the local var, thanks. I new this was there, I was just a little mixxed-up (better now).

I do still have another problem though and thats returning the collection members now. I could create a local var to do this like I had but would much rather return the collection

Any help would be great
Mike
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Stevens:
Thanks for looking in on me pascal. There were no errors it just was not behaving the way it was Intended. What do you mean by you should use code tags around any code you post.


About the code tags: When you put source code in your post, write it between [ code ] and [/ code ] tags (without the spaces, ofcourse) - then the forum software will format and indent it nicely. See this topic from the JavaRanch FAQ: Use Code Tags
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post your new version (including the code tags, as Jesper described) and explain again what you want to do and whats happening isntead ?

pascal
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for takin' a boo pascal. So the method is not complete yet but easy to finish the validation and some other thing it needs. I have it doing what I want it to do,

just wondering is if I can return the data from members.add(monthMem);?

rather than the local var?

purged.add(monthMem);

I have to use the method sig.


-----------------------------------------------------------
Class and constructor


-------------------------------------------------
Method



[ November 08, 2006: Message edited by: Mike Stevens ]
[ November 09, 2006: Message edited by: Mike Stevens ]
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can return whatever you want... as long as it fits the method signature (btw: i would recomend to use java.util.List instead of java.util.ArrayList as the return type. it is considered good practice to code against interfaces rather than implementations - if you are allowed to change this)

I amo not sure i follow what you want to do:
- you have a List "memberships". does this include all the members (valid date/invalid date) ?
- you have a List "members". is this a subset of "membership" including just the valid members (those which are not purged) ?
- in your purge method you drop the members depending on some date criteria and you want to return the purged collection ?

? did i get it right so far ?


If you need to "recalucalate" the purged List everytime (e.g. because once you purge for December 2006 and once you purge for January 2006) then you should not make the "members" List a instance variable. Rather it should be a method local variable. (in this case the "members" List is only valid when accessed trough the purge() method - it's content is dependent on the last purge() method call).

Also in your implementation if you call purge() twice, the same member might be in the "members" List twice (members.add() is called twice with the same member)






do i make any sense ? have a nice day.

pascal
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again pascal, I pretty much have to stick to what is posted above. Other than what I thought I would have to use



But with what you mentioned about what can be returned (thanks) and the error I received about incompatable types when I tried to return members it became clear that I need to change



All works now

This is from a lesson in a book. I was in an intro course for Java and OOP but had to drop out due to medical reasons so now I am left with no support for this book but really want to complete it.

The book is asking me to
Remove all members from a given month and return them stored in a separate collection. It is the seperate collection that I wanted to return and access in another method



There is a little support for the book, but the answers are not avalable so when your stuck or if your right or wrong you have know way of knowing because you have nothing to compare to. Easy to get off track even when things are working right.

Thanks for looking in, all is as it should be now. I hope!

Intil next time
Mike
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic