vector removeElementAt removes every second element

Discussion in 'Java' started by gbruno, Jun 6, 2005.

  1. gbruno

    gbruno Guest

    Before you use java Vector class, try this code:


    public void tevec() {
    Vector dat;
    int j;
    dat = new Vector();
    dat.clear();
    System.out.print("adding " );
    for (j = 0; j < 20; j++) {
    System.out.print(" " + j );
    dat.addElement(Integer.toString(j));
    }
    System.out.println(" looking ");
    for (j = 0; j < 20; j++) {
    System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    + "...");
    }
    for (j = 0; j < 5; j++) {
    dat.removeElementAt(j);
    }
    System.out.println(" after removing ");
    for (j = 0; j < 5; j++) {
    System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    + "...");
    }
    System.out.println(" how bad is that? ");

    } //
    tevec______________________________________________________________________



    adding 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 looking

    at 0...0...at 1...1...at 2...2...at 3...3...at 4...4...at 5...5...at
    6...6...at 7...7...at 8...8...at 9...9...at 10...10...at 11...11...at
    12...12...at 13...13...at 14...14...at 15...15...at 16...16...at
    17...17...at 18...18...at 19...19... after removing

    at 0...1...at 1...3...at 2...5...at 3...7...at 4...9... how bad is
    that?
    gbruno, Jun 6, 2005
    #1
    1. Advertising

  2. On 5 Jun 2005 23:12:22 -0700, gbruno wrote:

    > Before you use java Vector class, try this code:
    >
    >
    > public void tevec() {
    > Vector dat;
    > int j;
    > dat = new Vector();
    > dat.clear();
    > System.out.print("adding " );
    > for (j = 0; j < 20; j++) {
    > System.out.print(" " + j );
    > dat.addElement(Integer.toString(j));
    > }
    > System.out.println(" looking ");
    > for (j = 0; j < 20; j++) {
    > System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    > + "...");
    > }
    > for (j = 0; j < 5; j++) {


    Try..
    for (j = 5; j > -1; j--) {

    > dat.removeElementAt(j);
    > }


    ....

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jun 6, 2005
    #2
    1. Advertising

  3. gbruno wrote:
    > Before you use java Vector class, try this code:

    ....
    > for (j = 0; j < 5; j++) {
    > dat.removeElementAt(j);
    > }

    ....
    > at 0...1...at 1...3...at 2...5...at 3...7...at 4...9... how bad is
    > that?
    >


    Rather than trying the code, read Vector's documentation
    before using it. Move generally, read the documentation
    before using any class.

    removeElementAt "Deletes the component at the specified
    index. Each component in this vector with an index greater
    or equal to the specified index is shifted downward to have
    an index one smaller than the value it had previously. The
    size of this vector is decreased by 1."

    At the start of iteration j, the elements from index j on
    have been shifted down by j, due to the j prior
    removeElementAt calls, so it removes the element
    that was initially at index j+j.

    Patricia
    Patricia Shanahan, Jun 6, 2005
    #3
  4. Andrew Thompson wrote:
    > On 5 Jun 2005 23:12:22 -0700, gbruno wrote:
    >
    >
    >>Before you use java Vector class, try this code:
    >>

    ....
    >> for (j = 0; j < 5; j++) {

    >
    >
    > Try..
    > for (j = 5; j > -1; j--) {


    The original loop does 5 iterations (0,1,2,3,4), but this
    version does 6 (5,4,3,2,1,0). I'd have started it at 4, not 5.

    Patricia
    Patricia Shanahan, Jun 6, 2005
    #4
  5. On Mon, 06 Jun 2005 06:59:55 GMT, Patricia Shanahan wrote:
    > Andrew Thompson wrote:
    >> On 5 Jun 2005 23:12:22 -0700, gbruno wrote:
    >>
    >>>Before you use java Vector class, try this code:

    > ...
    >>> for (j = 0; j < 5; j++) {

    >>
    >> Try..
    >> for (j = 5; j > -1; j--) {

    >
    > The original loop does 5 iterations (0,1,2,3,4), but this
    > version does 6 (5,4,3,2,1,0). I'd have started it at 4, not 5.


    Where'd I leave that dang DWIMNWIS interpreter? ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jun 6, 2005
    #5
  6. gbruno

    Wibble Guest

    gbruno wrote:
    > Before you use java Vector class, try this code:
    >
    >
    > public void tevec() {
    > Vector dat;
    > int j;
    > dat = new Vector();
    > dat.clear();
    > System.out.print("adding " );
    > for (j = 0; j < 20; j++) {
    > System.out.print(" " + j );
    > dat.addElement(Integer.toString(j));
    > }
    > System.out.println(" looking ");
    > for (j = 0; j < 20; j++) {
    > System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    > + "...");
    > }
    > for (j = 0; j < 5; j++) {
    > dat.removeElementAt(j);
    > }
    > System.out.println(" after removing ");
    > for (j = 0; j < 5; j++) {
    > System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    > + "...");
    > }
    > System.out.println(" how bad is that? ");
    >
    > } //
    > tevec______________________________________________________________________
    >
    >
    >
    > adding 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 looking
    >
    > at 0...0...at 1...1...at 2...2...at 3...3...at 4...4...at 5...5...at
    > 6...6...at 7...7...at 8...8...at 9...9...at 10...10...at 11...11...at
    > 12...12...at 13...13...at 14...14...at 15...15...at 16...16...at
    > 17...17...at 18...18...at 19...19... after removing
    >
    > at 0...1...at 1...3...at 2...5...at 3...7...at 4...9... how bad is
    > that?
    >

    Thats a really funny bug. Hee, hee, hee.

    Oh, and O(n^2) too.
    Wibble, Jun 6, 2005
    #6
  7. gbruno

    Wibble Guest

    Wibble wrote:
    > gbruno wrote:
    >
    >> Before you use java Vector class, try this code:
    >>
    >>
    >> public void tevec() {
    >> Vector dat;
    >> int j;
    >> dat = new Vector();
    >> dat.clear();
    >> System.out.print("adding " );
    >> for (j = 0; j < 20; j++) {
    >> System.out.print(" " + j );
    >> dat.addElement(Integer.toString(j));
    >> }
    >> System.out.println(" looking ");
    >> for (j = 0; j < 20; j++) {
    >> System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    >> + "...");
    >> }
    >> for (j = 0; j < 5; j++) {
    >> dat.removeElementAt(j);
    >> }
    >> System.out.println(" after removing ");
    >> for (j = 0; j < 5; j++) {
    >> System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    >> + "...");
    >> }
    >> System.out.println(" how bad is that? ");
    >>
    >> } //
    >> tevec______________________________________________________________________
    >>
    >>
    >>
    >>
    >> adding 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 looking
    >>
    >> at 0...0...at 1...1...at 2...2...at 3...3...at 4...4...at 5...5...at
    >> 6...6...at 7...7...at 8...8...at 9...9...at 10...10...at 11...11...at
    >> 12...12...at 13...13...at 14...14...at 15...15...at 16...16...at
    >> 17...17...at 18...18...at 19...19... after removing
    >>
    >> at 0...1...at 1...3...at 2...5...at 3...7...at 4...9... how bad is
    >> that?
    >>

    > Thats a really funny bug. Hee, hee, hee.
    >
    > Oh, and O(n^2) too.

    Oh, for what its worth...
    for (j = 0; j < 5; j++) {
    dat.removeElementAt(0); // Note the 0, not j
    }
    Wibble, Jun 6, 2005
    #7
  8. gbruno

    gbruno Guest

    Re: vector removeElementAt removes every second element

    I knew that...
    OK I have removed my original posting,
    so posterity may not judge me harshly
    gbruno, Jun 7, 2005
    #8
  9. gbruno

    Angus Guest

    Andrew Thompson wrote:
    >
    > On 5 Jun 2005 23:12:22 -0700, gbruno wrote:
    >
    > > Before you use java Vector class, try this code:

    [SNIP]
    > > for (j = 0; j < 5; j++) {

    >
    > Try..
    > for (j = 5; j > -1; j--) {
    >
    > > dat.removeElementAt(j);
    > > }


    Actually, I believe the following is slightly more elegant (and
    slightly more correct :)

    for (j = 5; --j >= 0;) {
    dat.removeElementAt(j);
    }

    [It's probably slightly more efficient, too ;-]

    Good Luck,
    Avi.
    Angus, Jun 7, 2005
    #9
  10. gbruno

    steve Guest

    On Mon, 6 Jun 2005 14:12:22 +0800, gbruno wrote
    (in article <>):

    > Before you use java Vector class, try this code:
    >
    >
    > public void tevec() {
    > Vector dat;
    > int j;
    > dat = new Vector();
    > dat.clear();
    > System.out.print("adding " );
    > for (j = 0; j < 20; j++) {
    > System.out.print(" " + j );
    > dat.addElement(Integer.toString(j));
    > }
    > System.out.println(" looking ");
    > for (j = 0; j < 20; j++) {
    > System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    > + "...");
    > }
    > for (j = 0; j < 5; j++) {
    > dat.removeElementAt(j);
    > }
    > System.out.println(" after removing ");
    > for (j = 0; j < 5; j++) {
    > System.out.print("at " + j + "..." + (String)dat.elementAt(j)
    > + "...");
    > }
    > System.out.println(" how bad is that? ");
    >
    > } //
    > tevec______________________________________________________________________
    >
    >
    >
    > adding 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 looking
    >
    > at 0...0...at 1...1...at 2...2...at 3...3...at 4...4...at 5...5...at
    > 6...6...at 7...7...at 8...8...at 9...9...at 10...10...at 11...11...at
    > 12...12...at 13...13...at 14...14...at 15...15...at 16...16...at
    > 17...17...at 18...18...at 19...19... after removing
    >
    > at 0...1...at 1...3...at 2...5...at 3...7...at 4...9... how bad is
    > that?
    >


    Very Very funny.


    try making your remove loop count count backwards. I.E j-- not J++ and

    Steve
    steve, Jun 8, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Jude
    Replies:
    3
    Views:
    466
  2. peter leonard
    Replies:
    3
    Views:
    538
    Roy Smith
    Oct 20, 2003
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,434
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  5. bgold12
    Replies:
    7
    Views:
    109
    dhtml
    Sep 2, 2008
Loading...

Share This Page