Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 51
  1. #21
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Java Code:
    Name:Kaya                 Color:BROWN      Sex:Female     Age:2     DNA:Normal
    Name:Lizeth               Color:WHITE      Sex:Female     Age:2     DNA:Normal
    Name:Allen                Color:SPOTTED    Sex:Male       Age:2     DNA:Normal
    Name:Seamus               Color:WHITE      Sex:Male       Age:2     DNA:Normal
    Name:Chris                Color:GREY       Sex:Male       Age:2     DNA:Normal
    Total males: 3.
    Total females: 2.
    Total radioactive mutant vampires: 0.
    Exception in thread "main" java.util.ConcurrentModificationException
    	at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
    	at java.util.AbstractList$Itr.next(AbstractList.java:343)
    	at bunny.FieldOfBunnies.calculateOffspring(FieldOfBunnies.java:56)
    	at bunny.FieldOfBunnies.turn(FieldOfBunnies.java:39)
    	at bunny.BunnyMain.main(BunnyMain.java:28)

  2. #22
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Ignore Cbani, his "solution" is no such thing. I suspect they think ConcurrentModification is always down to synchronisation, when it isn't.

    The Iterator is what you need to use.
    You need to read the API for it.
    Specifically that next() returns the Object. That is, the item in the List.

    If my List is a List of Strings (List<String>) then next() will return me a String.

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

    Default

    Thanks Tolls, However I have tried a number of different ways around using the iterator. What I can't figure out is how to use the object returned by next as standard manipulation is giving errors. Could you please post a very simple sample something like:

    Java Code:
    for (Iterator it = bunnyList.iterator(); it.hasNext();) 
    {
       Bunny bun;
       bun=it.next();
       bun.setRadioactive(false);
    }
    I have tried, numerous different things. But without a working sample it just makes no sense.
    As you just stated it.next() should have returned a "Bunny Object" (or string in the case of List<String>) however assigning it to a tempory bunny object doesn't seem to work.

  4. #24
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    You really need to give us the error...but that code isn't using generics.

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

    Default

    In order for it to return a "Bunny Object" then it should, of course, be Iterator<Bunny> in the same way that List<String> is a List that returns (and only accepts) Strings. (All of this is not actually case however. It's all just for the compiler to do type checking. Whether it is List, or List<String> it will return a String, as long as a String is what was added to it, the compiler is just not able to recognise that without the Generics.)

    Edit: And, in order for you to use Iterator<Bunny> it must also be List<Bunny>, of course.

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

    Default

    Quote Originally Posted by Cbani View Post
    its very simple..change the list to a vector...everything works fine....
    or else if you dont want to do so then do as follows while constructing your list.

    List arl = Collections.synchronizedList(new ArrayList());
    WTH? this is bad advice. I agree with Tolls (and as per my previous note), use next() on the iterator to get your object.

  7. #27
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Java Code:
    for (Iterator<Bunny> it = bunnyList.iterator(); it.hasNext();) 
        Bunny bun = (Bunny)it.next();
    it cannot be resolved.
    bun cannot be resolved.
    Bunny cannot be resolved.
    Syntax error insert ";" to complete code.
    Syntax error insert "assignment operator expression" to complete code.
    Last edited by Newbie666; 01-22-2010 at 10:56 PM.

  8. #28
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    I still can't figure out why this won't work. To me seems as though it should/would.
    if iterator is of type Bunny, as the List is of type bunny, then how come I cannot assign to a bunny object ?
    Am I using the correct code?

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

    Default

    Are you compiling with Java compiler set at 1.5? If not, you can't use generics and will have to cast the object returned by the iterator as a Bunny.

    If still with problems, consider creating and posting an SSCCE.

  10. #30
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Compiler is set at 1.6.16 So I have no idea why it's not working. Every example I can find uses the same code I have come up with anyway.
    So the logic you guys have suggested is fine... But it's not working.

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

    Default

    This should work, I left out Color and radioactive... It at least gives an idea of what I'm doing, I have already pasted the line I'm getting an error on a few times. As for creating compilable code, I'm not sure I can without writing a whole new sample project...

    I have a feeling this has nothing to do with the code and there's something wrong with eclipse...However this has been my only error so far, well error that couldn't be fixed by correct syntax...

    Bunny object
    Java Code:
    public class Bunny
    {
        public Bunny()
        {
        }
        public Bunny(String name,boolean sex, int age)
        {
            setName(name);
            setSex(sex);
            setAge(age);
        }
        ...
        public String toString()
        {
            return String.format("Blah blah...");
        }
    field of bunnies
    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Scanner;
    
    public class FieldOfBunnies
    {
    	/*
    	 * class will create random 5 bunnies each turn.
    	 * class will process existing bunnies and make subsequent bunnies
    	 * based on existing sexes and ages.
             * For each mutant vampire bunny, will convert 1 bunny by sucking on it's blood!
             * Normal bunnies die at age 10, vampires at age 50.
    	 */
    
    	private static final String BOY_NAMES = "names/boynames.txt";
    	private static final String GIRL_NAMES = "names/girlnames.txt";
    	private List<String> boyNames = new ArrayList<String>(1000);
    	private List<String> girlNames = new ArrayList<String>(1000);
    	private List<Bunny> bunnyList = new ArrayList<Bunny>(1000);
    	
    	public void turn()
    	{
    		int bunniesPerTurn=5;
    		for (int i=0;i<bunniesPerTurn;i++)
    		{
    			generateBunny();
    		}
    		calculateOffspring();
    		for (Bunny bunny : bunnyList)
    			bunny.setAge(bunny.getAge()+1);
    		Collections.shuffle(bunnyList);
    	}
    	
    	public void calculateOffspring()
    	{
    		for (Iterator<Bunny> it = bunnyList.iterator(); it.hasNext();) 
    			Bunny bun = (Bunny)it.next();
            // this gives an error....
    /*
    it cannot be resolved.
    bun cannot be resolved.
    Bunny cannot be resolved.
    Syntax error insert ";" to complete code.
    Syntax error insert "assignment operator expression" to complete code.
    */
            }
    
            public void generateBunny()
            {
                Bunny bunny = new Bunny("fred",false,1);
                bunnyList.add(bunny);
            }
    }
    Last edited by Newbie666; 01-23-2010 at 01:34 AM.

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

    Default

    Currently I don't see how we can test your posted code or find out what is wrong. If you don't get a decent answer in the near future, you may do well to re-read the link on creating SSCCE's and try to create a one that complies with the specification.

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

    Default

    Uhm, I don't see a package declaration in either of those classes. They are in a package, aren't they? You haven't left them in the so-called "default" package, have you? If you have, I wouldn't doubt that that is your problem in this case.

  14. #34
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    OMG!
    That is silly, I created a whole new project just so I could post something compilable, Created a basic Bunny class with enum Name. created teh Enum class and a get method for giving a random name. and then field of bunnies, created method to generateBunny, then last but not least:
    turn() which used Iterator<bunny> <----- only this time, because I thought I would want to have more than jsut the 2 lines, as people will need to be able to compile it and run it for me. So I put curly braces around the block, and bingo no more error...!

    So this is INVALID:
    Java Code:
    for (Iterator<Bunny> it = bunnyList.iterator(); it.hasNext();)
       Bunny bun = (Bunny)it.next();
    THIS IS VALID
    Java Code:
    for (Iterator<Bunny> it = bunnyList.iterator(); it.hasNext();)
    {
        Bunny bun = (Bunny)it.next();
    }
    my lord, possibly the most stupid error I have ever encountered.

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

    Default

    Yet another reason, then, to always use braces, whether its a one-liner, or not.

  16. #36
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    I'm back to the original problem now, ConcurrentModificationException. So looks my code was not a complete waste... I'll see if I can get the same error with the sample.

  17. #37
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    test
    Java Code:
    public class Test
    {
    	public static void main(String[] args)
    	{
    		FieldOfBunnies FOB = new FieldOfBunnies();
    		FOB.turn();
    		FOB.printStuff();
    	}
    }
    field
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    
    public class FieldOfBunnies
    {
    	List<Bunny> bunnyList = new ArrayList<Bunny>(5);
    	private int count=0;
    	
            // create a bunny object, with 1 of 5 names.
    	public void generateBunny()
    	{
    		if (count==6)
    			count=0;
    		Bunny bunny = new Bunny(Names.getName(count++));
    		bunnyList.add(bunny);
    	}
            // generate 5 bunnies per turn, then calculate offspring.
    	public void turn()
    	{
    		for (int i=0; i<5; i++)
    			generateBunny();
    		calculateOffspring();
    	}
    
            // create new Bunny objects and add to list for every bunny named Frank or bob.
    	public void calculateOffspring()
    	{
    		for (Iterator<Bunny> it = bunnyList.iterator(); it.hasNext();)
    		{
    			Bunny bun = (Bunny)it.next();
    			if (bun.getName()==Names.valueOf("FRANK"));
    				generateBunny();
    			if (bun.getName()==Names.valueOf("BOB"));
    				generateBunny(); 
    		}
    	}
    // prints the list.
    	public void printStuff()
    	{
    		for (Bunny bunny : bunnyList)
    			System.out.println(bunny.toString());
    	}
    }
    enum
    Java Code:
    public enum Names
    {
    	FRANK, BOB, HENRY, SALLY, HOLLY, BLANK;
    	
    	public static Names getName(int value)
    	{
    		switch(value)
    		{
    			case 0:
    				return FRANK;
    			case 1:
    				return BOB;
    			case 2:
    				return HENRY;
    			case 3:
    				return SALLY;
    			case 4:
    				return HOLLY;
    			default:
    				return BLANK;
    		}
    	}
    }
    Bunny
    Java Code:
    public class Bunny
    {
    	private Names name; 
    	public Bunny(Names name)
    	{
    		setName(name);
    	}
    	public void setName(Names name)
    	{
    		this.name=name;
    	}
    	public Names getName()
    	{
    		return name;
    	}
    	public String toString()
    	{
    		return String.format("Name: %s", getName());
    	}
    }
    Last edited by Newbie666; 01-23-2010 at 11:29 AM.

  18. #38
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    The only way I can figure out how to get around this is using a multitude of counters(about 9 total) to count the number of female bunnies over 2 who aren't radioactive. count the number of radioactive, count the number of radioactive who are aged 50, count the number of non-mutants who are aged 10. Then do all my assignment this way. But this is a really long long long way around what in my mind should be an easy solution. So I'm guessing java is not so much of a high language when it comes to changing Lists, while Iterating over them.

    IMHO this doesn't make much sense as the hasNext() method should return true or false regardless if the list was changed or not... I don't see why Java Devs would write this as an exception. Why not just allow Iteration on an ever growing List? I did plan to eventually stop it.... around 1000 bunnies :D


    Edit: yes, I still have the original problem with the above posted code. Exception when adding elements to a List while iterating over the List.
    Last edited by Newbie666; 01-23-2010 at 02:33 PM.

  19. #39
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Newbie666 View Post
    The only way I can figure out how to get around this is using a multitude of counters(about 9 total) to count the number of female bunnies over 2 who aren't radioactive. count the number of radioactive, count the number of radioactive who are aged 50, count the number of non-mutants who are aged 10. Then do all my assignment this way. But this is a really long long long way around what in my mind should be an easy solution. So I'm guessing java is not so much of a high language when it comes to changing Lists, while Iterating over them.
    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
    Last edited by JosAH; 01-23-2010 at 03:02 PM.

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

Page 2 of 3 FirstFirst 123 LastLast

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
  •