Vector Question for Newbie

From: Paul P (paulpigott_at_earthlink.net)
Date: 12/30/03


Date: 30 Dec 2003 08:11:12 -0800

Greetings,

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[0] has the entry just added.

After adding the second one, Vector[0] and Vector[1] both have the
entry just added (the second entry).

After adding the third one, Vector[0], Vector[1] and Vector[2] 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
follows:

=====CODE BEGIN==============================================================
if ( ISArec.length() != 106 )
{
   cm = new CommMessages( "SplitX12File: (Second) ISA read failed for
" +
                          cui.GetFileName(),
                          cc.LIT_SYSERROR,
                          cc.SYS_IOEXCEPTION );
   cui.messages.add( nextMsgPos++, cm );
   System.out.println(cui.messages);
                                                                
   cm2 = new CommMessages("ISArec=" + ISArec, "A", 0 );
   cui.messages.add( nextMsgPos++, cm2 );
   System.out.println(cui.messages);
                                                                
   cm3 = new CommMessages("ISArec length=" + ISArec.length(), "B", 0
);
   cui.messages.add( nextMsgPos++, cm3 );
   System.out.println(cui.messages);
                                                                
   return;
}
=====CODE END================================================================

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
more readable):

=====OUTPUT BEGIN==============================================================
[CommMessages[SplitX12File: (Second) ISA read failed for
C:\Tran\hold\zip79.dir\
$$COPY.DAT, SYSTEM, 901]]

[CommMessages[ISArec=, A, 0], CommMessages[ISArec=, A, 0]]

[CommMessages[ISArec length=0, B, 0], CommMessages[ISArec length=0, B,
0], CommM
essages[ISArec length=0, B, 0]]
=====OUTPUT END================================================================

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.

TIA,

Paul



Relevant Pages

  • Re: Vector Question for Newbie
    ... > I am running into a very unusual problem when adding elements to a ... I'm using a Vector to accumulate messages for particular ... > entry just added. ... What exactly is the CommMessages class? ...
    (comp.lang.java)
  • Re: Vector Question for Newbie
    ... Paul P wrote: ... > entry just added. ... The test code works as I would expect it to. ... Don't use public instance variables like the messages variable you ...
    (comp.lang.java)