aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList Headaches. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Headaches." Watch "ArrayList Headaches." New topic
Author

ArrayList Headaches.

Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
I have three flat files, and two classes that manipulate them.
After the first class reads a line it passes to te second with tokenizes it down then creates an object from the tokens. Finally adding the object to one of three ArrayLists.

The first ArrayList is works perfect. The Third (using almost 100% identical code) When an object is added it replaces all objects that preceed it in the list..

So what should look like

1 MessageHeader
2 MessageBody
3 MessageTail

Looks like

3 MessageTail
3 MessageTail
3 MessageTail

when I iterate through the list.

I am uterrly confused and frustrated



This is sort of a re-post, my previous post was about Vectors.. I am not doing that anymore..

THANK YOU SMART PEOPLE!

[ September 14, 2004: Message edited by: Hosh Nasi ]
[ September 15, 2004: Message edited by: Hosh Nasi ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

have a sharp look at following line:

If no bell rings, have a look at the javadocs for that method.


http://home.arcor.de/hirnstrom/bewerbung
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
Yeah I posted that without looking. the (o,<obj> ;) was something I was testing.

It dosen't work right even after that.

When it is just <list>.add(<obj> ;)

I get the same issue.
[ September 15, 2004: Message edited by: Marilyn de Queiroz ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I don't read
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Originally posted by Hosh Nasi:
When it is just <list>.add(<obj> ;)

<list>.add(<obj>);


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
What does the code look like which reads the line and calls the method shown above?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

Perhaps you should look more closely at your StringTokenizer and the parameters you are passing in. What are you tokenizing on? Where is it in the string you are passing in?
[ September 15, 2004: Message edited by: Marilyn de Queiroz ]
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44



The final line of the program is a print statement print a few of the variables for the created object. This works. The tokenization is perfect, the string is passed by the calling class along with the list and sepperated by ":". I believe the bug is with the Add method. You see Above the "constructSelector" I have two almost identical mathods that do the dame thing. However the add functions perfectly. So I coppied the code from the working method and changed the arguements to match a selector.. No luck.

The add method works in adding the new object to the bottom, however it also replaces everything above it too?

::EDIT:: let me answer you question. I am tokenizing an un broken string seperated by ":" coming from a flat file that is read by the previous class using buffered read and readline.

[ September 15, 2004: Message edited by: Hosh Nasi ]
[ September 15, 2004: Message edited by: Hosh Nasi ]
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
I created an iterator to check what was happening after the object was added, this was the result (I also used one with the previous working methods)

flatFile:

1 MessageHeader
2 MessageBody
3 MessageTail

loop with iterator 1:

1 MessageHeader

loop with iterator 2:

2 MessageBody
2 MessageBody

loop with iterator 3:

3 MessageTail
3 MessageTail
3 MessageTail

Has anyone see this before?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
What is the scope of the ArrayList object. I get the impression that the loop calling the code you have posted is where the error lies. Are you restating the ArrayList index to zero on each loop?
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
Array List is created back in the main in another class, the only place where the list is appended too is the code above. Also, if I kept reseting the list wouldn't I only recieved back one value not copies of the same valuse for each item in the flat file, the addressing of the objects are unique as well.

This is the code that calls the previous Method


[ September 15, 2004: Message edited by: Hosh Nasi ]
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Ok, that looks good.

In the Selector class, are some of the fields static, perhaps?
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
Yes all the fields that are to be constructed Static.
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
Doh, I always over complicate things...

This was it. I was satisfiedthat that objects memmory location was diffrent that i never though about the variables...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Headaches.