Vector Question for Newbie
From: Paul P (paulpigott_at_earthlink.net)
Date: 30 Dec 2003 08:11:12 -0800
I am running into a very unusual problem when adding elements to a
vector. I'm using a Vector to accumulate messages for particular
datasets. There is one point where I have to add three messages to
the Vector at one time.
After adding the first one, Vector has the entry just added.
After adding the second one, Vector and Vector both have the
entry just added (the second entry).
After adding the third one, Vector, Vector and Vector all
have the entry just added (the third entry).
I don't know why this is happening.
I tried to replicate this problem in some test code and was not able
to. The test code works as I would expect it to. The code is as
if ( ISArec.length() != 106 )
cm = new CommMessages( "SplitX12File: (Second) ISA read failed for
cui.messages.add( nextMsgPos++, cm );
cm2 = new CommMessages("ISArec=" + ISArec, "A", 0 );
cui.messages.add( nextMsgPos++, cm2 );
cm3 = new CommMessages("ISArec length=" + ISArec.length(), "B", 0
cui.messages.add( nextMsgPos++, cm3 );
The field "nextMsgPos" is initially loaded with the size of the
Vector. This gives the next position where an object should be added.
The results of the code are here (I added some linespacing to make it
[CommMessages[SplitX12File: (Second) ISA read failed for
$$COPY.DAT, SYSTEM, 901]]
[CommMessages[ISArec=, A, 0], CommMessages[ISArec=, A, 0]]
[CommMessages[ISArec length=0, B, 0], CommMessages[ISArec length=0, B,
essages[ISArec length=0, B, 0]]
The vector is defined as
public Vector messages;
and then instantiated with
messages = new Vector();
I am relatively new to Java programming in general and Vectors in
particular. What I'm trying to do seems to be relatively
straightforward. It's just not doing what I expect. I imagine it's
some trivial little thing I'm missing, but I'm stumped. Any help
would be greatly appreciated.