my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Adding Object to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding Object to ArrayList" Watch "Adding Object to ArrayList" New topic
Author

Adding Object to ArrayList

Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
Okay I'm basically creating a program that makes a GUI version of a movie catalog that is given in a text file. I got it working to find any errors in any of the fields and such. If an entry passes and is valid I need to add it to an ArrayList and if an error is found it will add it to a separate ArrayList for errors. I have it running in a loop so that after each entry is scanned it adds to either the Valid or Error List. Now, the problem happens when doing ValidEntry.add(Movie) or ErrorList.add(Error). It appends the first occurence to the list properly, then the next time it basically overwrites the first entry and appends a new entry with the same information.

For example, say the first movie is Hackers and the second is Antitrust. After the first iteration of the loop the ArrayList will be:

Hackers

After the second time it looks like this:

Antitrust
Antitrust

If you need any code let me know. Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Yes, I think we'd need to take a look at the code to pinpoint this.


Ping & DNS - updated with new look and Ping home screen widget
Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
Ignore the s****y style and lack of commenting for now...




Edit by mw: Cleaned up language.
[ March 26, 2008: Message edited by: marc weber ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
You're adding the same Movie object for each movie, and just calling the setters to change title etc. Instead, you need to construct new Movie objects. Something like:
Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
But doesn't ArrayList.add(Object) append the object to the array? It shouldn't overwrite any existing objects.
Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
I tried doing:

ValidEntry.add(new Movie(Current.Title, Current.Year, ..., ..., ...));

And it wrote null in the strings and 0's in the Year and Ratings.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Instead of "Current.Title" etc. you should pass in the actual information, like

new Movie("good title", 2010, "Superb", "abominable", "DVD")

And yes, ArrayList.add appends the object at the end of the list.
Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
The thing is though, that I'm the movies from a text file and there is like 1500 entries, so I don't know the movie information. And if it appends the object, it shouldn't overwrite the previous entries, right? So, I'm confused whether there is an error in my Program.. cause like I've set a counter to (validCount) and tried to due the add(index,Object) method for ArrayLists but that doesn't work either.

I also set up a test PrintWriter that writes the Current Object to a text file [test.println(Current)] and it prints it out properly.

any ideas?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
I didn't mean to use string literals; I meant to replace those with variables that you fill with appropriate values during reading the file.


This should work too, assuming the code sets the various attributes in Current before constructing the the Movie object. Without seeing the Movie class it's a bit hard to tell what it does, though.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Phill McElroy:
But doesn't ArrayList.add(Object) append the object to the array? It shouldn't overwrite any existing objects.


No, ArrayList.add(Object) adds a reference to an object to the list. So you now have two references to the same object (the one you're working with and the one in the list). If you then modify the object (using the reference you are working with), the 'object' in the list also changes because it is the same object.
e.g
Does that make sense ?


Joanne
Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
I did try with the Movie(Current.Year, .....) and had nulls..

Joanne - I see what you're saying.. How can I get it to add the Current instance of the object itself? So that the list won't change.
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
Hey Phil, just create a new Movie object every time you start reading a new movie.

So at the top you would have:



Then each time you start reading a new movie you would have:



Then you can add that new Movie object reference to the ArrayList.

You might also want to localize a lot of the variables you are using to the local code block, rather than declaring them all at the top. That will probably help with clarity imo.
Phill McElroy
Greenhorn

Joined: Jan 28, 2007
Posts: 13
Brad.... You!

Thanks! It's funny because it makes sooooo much sense to have done that anyways... and I sort of had it, just not when I was reading the movie just after i initialized it..

Anyways, yeah, Great! Thanks man.
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
Cool. Glad I could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding Object to ArrayList
 
Similar Threads
Enhanced for loop
Microsoft buying Yahoo ?
ArrayList add method causing Memory Overrun
Skipping element addition in list
Performance issuse