vector removeElementAt removes every second element

G

gbruno

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?
 
A

Andrew Thompson

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);
}

....
 
P

Patricia Shanahan

gbruno said:
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
 
P

Patricia Shanahan

Andrew said:
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
 
A

Andrew Thompson

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? ;-)
 
W

Wibble

gbruno said:
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.
 
W

Wibble

Wibble said:
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
}
 
G

gbruno

I knew that...
OK I have removed my original posting,
so posterity may not judge me harshly
 
A

Angus

Andrew said:
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.
 
S

steve

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top