• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding To An ArrayList  RSS feed

 
Jude Wyatt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all-

This is my first post here. I've been taking courses to learn Java for almost a year now. I really enjoy it and love it when I make a breakthrough. However, I'm struggling pretty hard right now with this one thing.

I have an ArrayList of objects in my Driver Program. I am required to pass this array to a method called "mate", which is located in another class. Inside this "mate" method, I'm required to check some conditions and if true, add additional objects to the ArrayList located in main. However, the method is not suppossed to return any value. The code I'm currently using is below:

This is my driver with the ArrayList, as well as the Main Method: (NOTE: there is quite a bit of other code that I have omitted because all I'm concerned with is learning how to add an additional object to my ArrayList, from the mate method.)



and here is my Mouse class which holds the grow() and mate() methods: (NOTE: Again, code omitted because all I'm concerned with is learning how to add an additional object to my ArrayList, from within the mate method.)



So, basically I need to know why this line miceModified.add(new Mouse()); is causing the error and what I need to do instead of this line in order to add an additional Mouse Object to my ArrayList, while I'm inside of this mate() method.

Here is the error that I receive:



Any help anyone could provide would be awesome!

Thanks,
chief


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

When you post code, UseCodeTags(⇐click) so it will be readable.

ItDoesntWorkIsUseless.(⇐click) You need to copy/paste the exact, complete error message.

As it is, skimming your unformatted code and with no details about the error, I don't see anything that should be causing a problem, so please follow the above suggestions and then we'll see what the problem is.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chief warpaint wrote:So, basically I need to know why this line miceModified.add(new Mouse()); is causing the error

What error ?
 
Jude Wyatt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Hi, and welcome to the Ranch!

When you post code, UseCodeTags(⇐click) so it will be readable.

ItDoesntWorkIsUseless.(⇐click) You need to copy/paste the exact, complete error message.

As it is, skimming your unformatted code and with no details about the error, I don't see anything that should be causing a problem, so please follow the above suggestions and then we'll see what the problem is.


Jeff-

My apologies and thanks for the heads up. I edited the post to reflect java code, but is there anyway to post the error message without having to type it all out? Copy/Paste doesn't seem to work.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chief warpaint wrote:is there anyway to post the error message without having to type it all out? Copy/Paste doesn't seem to work.


Windows cmd.exe?
Either just select it and hit Enter to copy, or, if not in quick-edit mode, right click, and choose "Select" or whatever is appropriate from the popup menu, then, I think, hit Enter to copy.

Linux terminal?
Just select the text to copy it.

Console in an IDE such as Eclipse or NetBeans?
You shouldn't be using an IDE yet, but I'm sure there's a way to copy its output. See its docs or help section.

Also, please re-edit your post so that the long comment is above the line in question, not at the end of it. Code doesn't line-wrap, so it makess the text area very wide and I have to scroll right to read everything. Thanks.
 
Jude Wyatt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
chief warpaint wrote:is there anyway to post the error message without having to type it all out? Copy/Paste doesn't seem to work.


Windows cmd.exe?
Either just select it and hit Enter to copy, or, if not in quick-edit mode, right click, and choose "Select" or whatever is appropriate from the popup menu, then, I think, hit Enter to copy.

Linux terminal?
Just select the text to copy it.

Console in an IDE such as Eclipse or NetBeans?
You shouldn't be using an IDE yet, but I'm sure there's a way to copy its output. See its docs or help section.

Also, please re-edit your post so that the long comment is above the line in question, not at the end of it. Code doesn't line-wrap, so it makess the text area very wide and I have to scroll right to read everything. Thanks.


Jeff-

Thanks. That worked perfectly! I posted the error message into the original post.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Did you read the docs for java.util.ConcurrentModificationException?(⇐click)
"For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception."

I didn't see any multithreading in your code, so probably you're doing this:



While an iteration is in progress, you can only modify the list via the iterator. Plain old Iterator has a remove() method. ListIterator has an add() method as well.

So, you either need to use a ListIterator and call its add() method, rather than the List's add() method; or you need to collect all the stuff to add in a separate List, and then, after the iteration is complete, add everything from the new list into the original.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The foreach loop is just syntactic sugar for looping over a collection with an Iterator (see this thread for an example). And modifying a collection when it is being iterated using anything other than the itearor is not allowed.
As it says in the javadoc for ArrayList
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That grow method, if you have the same in the Cat class, will have this effect
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to introduce some new lines into your code because the comment made it too wide for a screen.
Why is the mouseModified List static? Why are you replacing it in your mate() method? Both those things look like very iffy design to me.

You can only suffer that exception if you have an Iterator elsewhere. It must be that in the for-each loop in the main method, passed on to mate() method. If you do need Iterators, you should almost always declare them as loop-local variables, not fields, otherwise you are setting yourself up for a ConcurrentModificationException. In the case where you wish to alter the List, you should consider using an old-fashioned for loop, not for-each. Also read about the RandomAccess [←link] interface, and see whether ArrayList implements RandomAccess.
 
Jude Wyatt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...Why is the mouseModified List static? Why are you replacing it in your mate() method? Both those things look like very iffy design to me...


Campbell-

That is from a result of me tinkering. We're not suppossed to return anything from the mate() method. So , essentially I tried making a copy of the ArrayList and adding new Mouse Objects to the copy. Then at a later time, I would pull the copied ArrayedList for generating screen output. Like the title of the post says, I'm a newb and am still learning all of this. I'm sure there is probably a better way to do this. I just haven't learned it yet.


 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 16 of your Mouse class does not create a copy of m, it creates a new reference to the existing List object.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get a pencil and paper (and an eraser, probably the most important piece of equipment) and draw a diagram with your classes on. Draw a List of Mice; a square labelled mouseList containing M1, M2, M3, will do. Now decide where to put that.
  • Does it belong to a particuar mouse (instance field of the mouse class)?
  • Does it belong to the application as a whole (passed from outside the Mouse class)?
  • Does it belong to the Mouse class (rather than to a particular Mouse), being shared between all Mice (probably static field of the mouse class)?
  • Altering the field because a List is passed as an argument sounds iffy to me. Maybe you should have a method like this:-
    If you search these fora, you will find discussion about whether things should be static or not; there seem to have been lots of such discussion in the last week or two. Making something static “because it works” sounds dubious, but making something static because you have worked out it ought to be static is good practice.
    You are right not to return something from the mate() method. A method should do one thing and one thing only.
    There are ways of granting access to the List of mice from outside the class; you create a method which returns a reference to it (or, better still, you find this, which copies it in a read-only form, to return from your method). By the way: if you are using a static member, you should call it by the name of the class, not an object. If you have a method which returns something static, you will most likely want that method to be static, too.
     
    Jude Wyatt
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:That grow method, if you have the same in the Cat class, will have this effect


    Campbell-

    Hahaha you were right. The kitty sure does get big pretty quick. Any way you could explain to me why it is happening and how I can correct it? I do have a grow method in both classes, but I thought that I was telling them to call their appropriate classes grow methods.

    Thanks,
    Jeff
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I actually meant the effect at the end of the clip.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!