Page 3 of 3 FirstFirst 123
Results 41 to 51 of 51
  1. #41
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First off: bad if blocks:
    Java Code:
    			Bunny bun = (Bunny)it.next();
    			if (bun.getName()==Names.valueOf("FRANK"));
    				generateBunny();
    			if (bun.getName()==Names.valueOf("BOB"));
    				generateBunny();
    What do you think the semicolons at the end of each line starting with if... is going to do? ;)
    Next off: you're comparing Strings with == rather than equals(...) or equalsIgnoreCase(...) methods and this too will bite you in the end.

    On to your problem. You're trying to add to the list during an iteration which is dangerous. How about creating a temp List<Bunny> and adding to it inside the for loop. Then after the loop has ended use List.addAll to combine lists.

    edit: nevermind, I was thinking of the Iterator interface, not the ListIterator interface. Josah has your answer.

  2. #42
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You obviously haven't read the Collections tutorial otherwise you wouldn't have said this. Have a look at the ListIterator; it can add, change and remove elements of a list while iterating over it. If something you want isn't implemented by something you've randomly picked it is not the blame of that thing you picked. It was your incorrect selection, inspired by your lack of knowledge about the subject.

    kind regards,

    Jos
    Your right, I was going by what other people said instead of taking initiative to research myself, however I had not used Collections before or read about them. However many times stated they are typically better than arrays, so I made silly assumptions based on this but was also fueled by other conflicting replies.

  3. #43
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Changed to ListIterator and problem solved, exactly what I wanted. Thanks.

    Changed 3 methods in the above code:
    Java Code:
            public Bunny generateBunny()
    	{
    		if (count==6)
    			count=0;
    		Bunny bunny = new Bunny(Names.getName(count++));
    		return bunny;
    	}
    	public void turn()
    	{
    		for (int i=0; i<5; i++)
    			bunnyList.add(generateBunny());
    		calculateOffspring();
    			
    	}
    	public void calculateOffspring()
    	{
    		for (ListIterator<Bunny> it = bunnyList.listIterator(); it.hasNext();)
    		{
    			Bunny bun = (Bunny)it.next();
    			if (bun.getName()==Names.valueOf("FRANK"))
    				it.add(generateBunny());
    			if (bun.getName()==Names.valueOf("BOB"))
    				it.add(generateBunny());
    				
    		}
    	}
    Last edited by Newbie666; 01-24-2010 at 05:58 AM.

  4. #44
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    you're comparing Strings with == rather than equals(...)
    Am I not comparing enums?
    Java Code:
    if (bun.getName()==Names.valueOf("FRANK"))
    getName() returns enum 'Name' & Names.valueOf("FRANK") should be like a temporary enum ?

  5. #45
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My bad. If getName() returns an enum then == should work just fine.

  6. #46
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    bunnyList.remove(bunny);


    this piece of code is making your program failing...
    the reason is you are iterating through bunnyList and meanwhile inside the loop you are trying to remove one of the element from bunnyList.which is illegal....

    so to overcome this you probably could do a "break" to get out of the loop; if it fits your requirement..To be frank enough you could not do a remove of the same list inside the loop which you are iterating through

  7. #47
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Too little, too late.

  8. #48
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    To be honest, though you've sorted the problem, I wouldn't go adding bunnies to the list as you're iterating. I'd count up how many Frank's and Bob's you have then, after finishing that iterating, do a loop to add new bunnies. No real reason, though if you're interested in the order bunnies are added this would be important (ListIterator's add() will stick them before the current bunny and not at the end of the List).

  9. #49
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    To be honest, though you've sorted the problem, I wouldn't go adding bunnies to the list as you're iterating. I'd count up how many Frank's and Bob's you have then, after finishing that iterating, do a loop to add new bunnies. No real reason, though if you're interested in the order bunnies are added this would be important (ListIterator's add() will stick them before the current bunny and not at the end of the List).
    Yea, of course I see your point. (Although it could be fixed with a sort), however for the call on the vampire to convert I'm doing a shuffle() anyway so all the vampires aren't at the start of the list.

    I finished the project, this was pretty much the only thing holding me back. However the call to generate 2% of bunnies as vampires seem to work far too often I'd end up with a full field of vampires only after 10 turns. I'm not sure if this was a pseudo random generation problem or not, but some turns I would get 2 or 3 vampires out of the 5 I generated, which just seems virtually impossible... Anyway, that's done and I learned a lot about Collections and iterator. Thanks too everyone.

    as a side note if anyone see's a problem with this ?
    Java Code:
    public double getRandom(int numOutOf)
    {
    	double temp = Math.random();
    	return (temp *= numOutOf);
    }
    and typically when calling I would cast to int, eg. in the case of vampires;
    int temp = (int)getRandom(100);
    if (temp< 2)
    bunny is vampire.
    else
    bunny is normal.
    Last edited by Newbie666; 01-25-2010 at 04:45 PM.

  10. #50
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Store a Random object, and use nextInt(int)?

  11. #51
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Replies: 19
    Last Post: 08-28-2009, 12:49 AM
  2. ConcurrentModificationException with ArrayList
    By fogus in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •