Java iteration...how?

Talk about computers, hardware, applications, and consumer electronics.
User avatar
Demon Fisherman
Posts: 1246
Joined: 2008.10.01 (23:37)
NUMA Profile: http://nmaps.net/squibbles
MBTI Type: ENFP
Location: Canberra

Postby squibbles » 2009.03.26 (03:26)

Right, once again, I've hit a wall in my code, I know what the problem is, and I know in need an Iterator, but I have no idea how to implement it... This is my code:

Code: Select all

public class Club
{
    public ArrayList<Membership> Member;
    public ArrayList<Membership> PurgedMembers;
    
    /**
     * Constructor for objects of class Club
     */
    public Club()
    {
        Member = new ArrayList<Membership>();
        PurgedMembers = new ArrayList<Membership>();
    }

    public ArrayList<Membership> purge(int month, int year)
    {
        if(month >= 1 || month >= 12)
        {
            for(Membership member : Member)
            {
                if(member.month == month && member.year == year)
                {
                    PurgedMembers.add(member);
                    Member.remove(member);
                }
            }
        }
        else
        {
            System.out.println("Month must be between 1 and 12. Your entry of " + month + " does not comply with this condition.");   
        }
        return PurgedMembers;
    }
}
There is more, but, well, it doesn't affect this part of code...just more functions.

Any help would be much appreciated.
spoiler

Nmaps.net

Tsukatu wrote:I don't know what it is, squibbles, but my brain keeps inserting "black" into random parts of your posts these days.
I totally just read that as, "I'd hate to be the only black guy stuck using v1.4."
[/ispoiler]

User avatar
Damn You're Fine
Posts: 384
Joined: 2008.09.19 (01:47)
NUMA Profile: http://nmaps.net/user/littleviking001
MBTI Type: INTP
Location: Southern California
Contact:

Postby LittleViking » 2009.03.26 (03:39)

Problem:

public ArrayList<Membership> purge(int month, int year)
{
if(month >= 1 || month >= 12)
{

What do you need an iterator for here, by the way?
Image
The last Metroid is in captivity. The galaxy is at peace...

User avatar
Depressing
Posts: 1977
Joined: 2008.09.26 (06:46)
NUMA Profile: http://nmaps.net/user/rennaT
MBTI Type: ISTJ
Location: Trenton, Ontario, Canada
Contact:

Postby Tanner » 2009.03.26 (03:47)

You also need to make certain that all your variables have the same capitalization and change if(member.month == month && member.year == year) to if((member.month == month) && (member.year == year)).
Image
'rret donc d'niaser 'vec mon sirop d'erable, calis, si j't'r'vois icitte j'pellerais la police, tu l'veras l'criss de poutine de cul t'auras en prison, tabarnak

User avatar
Demon Fisherman
Posts: 1246
Joined: 2008.10.01 (23:37)
NUMA Profile: http://nmaps.net/squibbles
MBTI Type: ENFP
Location: Canberra

Postby squibbles » 2009.03.26 (04:01)

The error i get is:

cannot remove from an array list whilst iterating over it...My teacher said that using a separate iterator would solve this problem.

I feel i should explain the names as well:

Member is an arraylist which contains three fields per membership:

member
year
month

Code: Select all

if(month >= 1 || month >= 12)
This is simply in there to return an error if month (the variable input by the user) is not a valid month. What's wrong with it?
spoiler

Nmaps.net

Tsukatu wrote:I don't know what it is, squibbles, but my brain keeps inserting "black" into random parts of your posts these days.
I totally just read that as, "I'd hate to be the only black guy stuck using v1.4."
[/ispoiler]

User avatar
Damn You're Fine
Posts: 384
Joined: 2008.09.19 (01:47)
NUMA Profile: http://nmaps.net/user/littleviking001
MBTI Type: INTP
Location: Southern California
Contact:

Postby LittleViking » 2009.03.26 (04:37)

squibbles wrote:

Code: Select all

if(month >= 1 || month >= 12)
This is simply in there to return an error if month (the variable input by the user) is not a valid month. What's wrong with it?
You're checking if the month is greater than 1 or greater than 12. You probably want to check if the month is less than 1 or greater than 12.
Image
The last Metroid is in captivity. The galaxy is at peace...

User avatar
Demon Fisherman
Posts: 1246
Joined: 2008.10.01 (23:37)
NUMA Profile: http://nmaps.net/squibbles
MBTI Type: ENFP
Location: Canberra

Postby squibbles » 2009.03.26 (05:21)

*facepalm*

I can't believe I was that dumb. :(

I still however get the same error as earlier with the for-each loop, saying I cannot remove an entry from a collection while iterating over it.
spoiler

Nmaps.net

Tsukatu wrote:I don't know what it is, squibbles, but my brain keeps inserting "black" into random parts of your posts these days.
I totally just read that as, "I'd hate to be the only black guy stuck using v1.4."
[/ispoiler]

User avatar
Global Mod
Global Mod
Posts: 1416
Joined: 2008.09.26 (05:35)
NUMA Profile: http://nmaps.net/user/scythe33
MBTI Type: ENTP
Location: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Postby scythe » 2009.03.26 (07:23)

LittleViking wrote:
squibbles wrote:

Code: Select all

if(month >= 1 || month >= 12)
This is simply in there to return an error if month (the variable input by the user) is not a valid month. What's wrong with it?
You're checking if the month is greater than 1 or greater than 12. You probably want to check if the month is less than 1 or greater than 12.
Also, he might want to remove the "or equal to". Last I checked, both January and December were months.

If I may comment on your style, PurgedMembers should probably not be a class variable; just declare it in your function and return it. Unless you plan to make other functions read and write to that same variable; in which case, I'd say make your function return "void" (PurgedMembers is already public).
As soon as we wish to be happier, we are no longer happy.

User avatar
Retrofuturist
Posts: 3131
Joined: 2008.09.19 (06:55)
MBTI Type: ENTP
Location: California, USA
Contact:

Postby t̷s͢uk̕a͡t͜ư » 2009.03.26 (07:46)

I don't actually know any Java, but if it's anything like C++ in the way it handles more advanced data structures, it's often a very good idea / necessary to use an iterator when you're using data structure templates (and your declaration of ArrayList<Membership> looks an awful lot like that).
In C++, every STL data structure has an iterator class that you treat something like a pointer.
Quick example:

Code: Select all

vector<int> numbers;
// push_back() crap to numbers
vector<int>::iterator num_iter;
for (num_iter = numbers.begin(); num_iter != numbers.end(); num_iter++)
    cout << (*num_iter) << endl;
Sounds like there's something similar in Java?
[spoiler="you know i always joked that it would be scary as hell to run into DMX in a dark ally, but secretly when i say 'DMX' i really mean 'Tsukatu'." -kai]"... and when i say 'scary as hell' i really mean 'tight pink shirt'." -kai[/spoiler][/i]
spoiler

Image


Legacy Elite
Legacy Elite
Posts: 38
Joined: 2008.10.01 (01:49)

Postby Qix » 2009.03.27 (01:03)

Your problem is that using a for-each loop limits the functional of your iterator. If you attempt to remove something from a collection while iterating, the iterator loses track of the spot in the ArrayList. Instead you need to create an Iterator seperately:

Code: Select all

Iterator iter = Member.iterator()
 while (iter.hasNext()) {
Membership m = iter.Next();
PurgedMembers.add(m); //I assume there is no specific order to the list?
iter.remove();
}
Using iterator's remove method allows the iterator to keep track of the changes within the arraylist. Hope this helps.
Last edited by Qix on 2009.03.28 (13:58), edited 1 time in total.

User avatar
Ego Lancer
Posts: 303
Joined: 2008.09.26 (06:13)
NUMA Profile: http://nmaps.net/user/PsychoSnail
MBTI Type: ISTP
Location: The Gaming subforum

Postby PsychoSnail » 2009.03.28 (07:29)

squibbles wrote:I still however get the same error as earlier with the for-each loop, saying I cannot remove an entry from a collection while iterating over it.
I actually ran into nearly the exact same problem just a few weeks ago.
The way I solved it (since I know nearly nothing about Iterators) is I used a regular for loop, and if I removed an entry, I set the index value back one at the end of the loop body (so when 1 is added to the index value it ends up that nothing is changed), because the elements in an ArrayList move over to fill up the space that used to be filled by the element you just removed, so counting resumes from the place you were already at.
I'm talking about something like this:

Code: Select all

for(int i = 0; i < Member.size(); i++)
{
     Membership member = Member.get(i);
     if(member.month == month && member.year == year)
     {
           PurgedMembers.add(member);
           Member.remove(i);
           i--;
     }
}
I have no idea if it's better to use Iterators, as I've never used one before.

Edit: Looking at Qix's code, it looks to me that using an Iterator would be easier.

Edit 2:
scythe33 wrote:Also, he might want to remove the "or equal to". Last I checked, both January and December were months.

If I may comment on your style, PurgedMembers should probably not be a class variable; just declare it in your function and return it. Unless you plan to make other functions read and write to that same variable; in which case, I'd say make your function return "void" (PurgedMembers is already public).
QFE.
spoiler

Image
Opera innovates, Firefox emulates.
Last updated: September 27th, 2009


User avatar
Demon Fisherman
Posts: 1246
Joined: 2008.10.01 (23:37)
NUMA Profile: http://nmaps.net/squibbles
MBTI Type: ENFP
Location: Canberra

Postby squibbles » 2009.03.29 (23:35)

Yeah, thanks for the help guys, the problem has been resolved. :D
spoiler

Nmaps.net

Tsukatu wrote:I don't know what it is, squibbles, but my brain keeps inserting "black" into random parts of your posts these days.
I totally just read that as, "I'd hate to be the only black guy stuck using v1.4."
[/ispoiler]


Who is online

Users browsing this forum: No registered users and 10 guests