• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing from an array

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help. I don't know how to tackle this. I want to purge the members from the array list.



This is the entire code:



Can anybody shed some light?
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also needs to return it elsewhere, I can worry about that later though.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it more complex than this?

I suggested removeAll() just because I never have before and it lets you use the same "for(Membership memberList : members)" syntax you had on your counting methods.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i fail to see any significant difference between this thread and the one i closed over here, so correct me if i'm wrong.

changing from the suggested for loop to a while loop should be trivial. Stan's suggestion looks reasonable to me - can you explain why it won't work?
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm not allowed to use the for. Otherwise, I'd have it already.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the same as
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I say size of? My collection is members.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K, i got it... may have a question in a bit... depends on how much more I can figure out.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I take the month info from the user and see if that month is in the memberlist month? If it is remove it that member.

/**
* Remove from the club�s collection all members who joined in the given month, and
* return them stored in a separate collection object.
* @param month The month of the Membership.
* @param year The year of the Membership.
* @return The members who joined in the given month.
*/
public ArrayList<Membership> purge(int month, int year)
{
int i = 0;
while (i < members.size())
{
for(Membership memberList : members)
memberList.getMonth() = month;
}
return null;
}
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i handed you a piece of paper and said "here is a list of people. cross off every person with the first name of 'fred'", how would you do it?
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm thinking like this:
public ArrayList<Membership> purge(int month, int year)
{
int i = 0;
while (i < members.size())
{
for(Membership memberList : members)
if (memberList.getMonth() == month && memberList.getYear() == year)
memberList.remove(purge);
}
return null;
}
However, the memberList.remove(purge); says it cannot find the variable purge.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, it compiles with this:

public ArrayList<Membership> purge(int month, int year)
{
int i = 0;
while (i < members.size())
{
for(Membership memberList : members)
if (memberList.getMonth() == month && memberList.getYear() == year)
members.remove(memberList);
else
return members;
}
return members;
}

However, because of the for loop inside the while, it will run the machine in the compiler and freeze it. So, how can I call the month can year without the method i have just used?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i'm not sure what you're trying to do here, or that you really understand what's going on.

in your method, you need to look at ever member in you collection. as you look at each member, you need to decide if they should stay (in which case you'd do nothing) or go (in which case you need to do a couple of things).

perhaps you should back up a step. for starters, just get your method to print the month and year you want to test against, and print the name, month and year of EVERY member. in other words, try to get it to print

month: June
year: 2002

bob johns april 2001
sally smith may 2001
peter tork may 2001
davy Jones June 2002
mickey dolenz June 2002
michael nesmith august 2002



once you have that working, we'll move forward.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the for/in loop is order n squared and may not be freezing, but rather taking so long that it appears to freeze. I believe that there are really two loops. The for/in loop that you have coded and the loop internal to the ArrayList object that removes the element to be removed, but also moves everything down to pack the internal array so that there are no holes in the array.

What if you copied the ArrayList into a LinkedList, did the deletions in the Linked List, cleared the ArrayList, and added the elements of the Linked List back into the ArrayList. This sounds slower, but I believe that it may be quite a bit faster especially for large collections. I believe that this algorithm is order n.

-- Kaydell
 
reply
    Bookmark Topic Watch Topic
  • New Topic