Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

ArrayList Headaches.

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a sharp look at following line:

If no bell rings, have a look at the javadocs for that method.
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't read
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Hosh Nasi:
When it is just <list>.add(<obj> ;)


<list>.add(<obj>);
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the code look like which reads the line and calls the method shown above?
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that looks good.

In the Selector class, are some of the fields static, perhaps?
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes all the fields that are to be constructed Static.
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!