Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Vector - please help (urgent)

0 views
Skip to first unread message

ahansen

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to Patric Lehnen

> display items of a vector:
> System.out.println("display");
> for (int i=0; i<4; i++) {
> System.out.println("i2 = " + i);
> message.addElement(messages.elementAt(i));
> System.out.println("MessageQueue2 = " + message);
> System.out.println("i2 = " + i);
> messages.removeElementAt(i);
> System.out.println("MessageItem <" + messages.elementAt(i) + ">
> removed");

> java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
> at java.util.Vector.elementAt(Vector.java)
> at Producer3.getMessage(Producer3.java:58)
> at Consumer3.run(Consumer3.java:30)
> at java.lang.Thread.run(Thread.java)


Sure, when you removed element 0, element 1 became element 0 and the
Vector size dropped down to 3.

Change your 'for' stmt to following (note the results will be in
reverse):
for (int i = messages.size(); --i > 0; )

Patric Lehnen

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to

Am I tired or is it a bug in JDK 1.02?
Please help me !!! (I am totally despaired).

With the following code (only an excerpt)

fill vector with objects:
Integer temp = null;
System.out.println("store");
for (i=0; i<4; i++) {
System.out.println("i = " + i);
temp = new Integer(i);
messages.addElement(temp);System.out.println("MessageQueueIndex
= " + messages.indexOf(temp));
System.out.println("MessageQueueLength = " + messages.size());
System.out.println("MessageQueue = " + messages);


display items of a vector:
System.out.println("display");
for (int i=0; i<4; i++) {
System.out.println("i2 = " + i);
message.addElement(messages.elementAt(i));
System.out.println("MessageQueue2 = " + message);
System.out.println("i2 = " + i);
messages.removeElementAt(i);
System.out.println("MessageItem <" + messages.elementAt(i) + ">
removed");

I get the following output

store:
i = 0
MessageQueueIndex = 0
MessageQueueLength = 1
MessageQueue = [0]
i = 1
MessageQueueIndex = 1
MessageQueueLength = 2
MessageQueue = [0, 1]
i = 2
MessageQueueIndex = 2
MessageQueueLength = 3
MessageQueue = [0, 1, 2]
i = 3
MessageQueueIndex = 3
MessageQueueLength = 4
MessageQueue = [0, 1, 2, 3]

display:
i2 = 0
MessageQueue2 = [0]
i2 = 0
MessageItem <1> removed
i2 = 1
MessageQueue2 = [0, 2]
i2 = 1
MessageItem <3> removed
i2 = 2

Chris Kahrs

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to Patric Lehnen


Vectors in this way are very much like arrays (although you never herd
me say that!). Once you remove the elementAt(i) twice the only thing
left in the messages Vector is [1,3]. So trying to access position #3
is an error!
By the way, I always use Enumerations with Vectors and Hashtables as
this is a cleaner way to handle looping through these structures.

Hope this helps, (if not then disregaurd!)
Chris Kahrs...

Brad Koehn

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to

> Am I tired or is it a bug in JDK 1.02?
> Please help me !!! (I am totally despaired).
>
> With the following code (only an excerpt)
>
> fill vector with objects:
> Integer temp = null;
> System.out.println("store");
> for (i=0; i<4; i++) {
> System.out.println("i = " + i);
> temp = new Integer(i);
> messages.addElement(temp);System.out.println("MessageQueueIndex
> = " + messages.indexOf(temp));
> System.out.println("MessageQueueLength = " + messages.size());
> System.out.println("MessageQueue = " + messages);
>
>
> display items of a vector:
> System.out.println("display");
> for (int i=0; i<4; i++) {
> System.out.println("i2 = " + i);
> message.addElement(messages.elementAt(i));
> System.out.println("MessageQueue2 = " + message);
> System.out.println("i2 = " + i);
> messages.removeElementAt(i);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This line should read:
messages.removeElementAt(0);

Think about this. If you delete the nth element in a list, then all of the
elements after n get promoted, the n+1'th element becomes the nth, the
n+2'th element becomes the n+1'th, etc.

Make sense? To answer your original question: yes, you are tired.

___________________________________________________________________________
Brad Koehn Koehn Consulting br...@koehn.com
608.244.0153 http://www.mailbag.com/users/koehn/

Link the potential of technology to the reality of your business. (SM)


Bill Seurer

unread,
Oct 24, 1996, 3:00:00 AM10/24/96
to

In article <326E42...@zditf2.arcs.ac.at>, Patric Lehnen <leh...@zditf2.arcs.ac.at> writes:
|> Am I tired or is it a bug in JDK 1.02?
|> Please help me !!! (I am totally despaired).
|> System.out.println("display");
|> for (int i=0; i<4; i++) {
|> System.out.println("i2 = " + i);
|> message.addElement(messages.elementAt(i));
|> System.out.println("MessageQueue2 = " + message);
|> System.out.println("i2 = " + i);
|> messages.removeElementAt(i);

Right above is your bug. You remove the 0th element from the vector
the first time. Now the elements are 0:1, 1:2, and 2:3.
The next time your remove the element at index 1 leaving 0:1 and 1:3.
Then you try to remove the 2nd element. There is no 2nd element.

And thus you get:

|> java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
|> at java.util.Vector.elementAt(Vector.java)
|> at Producer3.getMessage(Producer3.java:58)
|> at Consumer3.run(Consumer3.java:30)
|> at java.lang.Thread.run(Thread.java)

--

- Bill Seurer ID Tools and Compiler Development IBM Rochester, MN
Business: BillS...@vnet.ibm.com Home: BillS...@aol.com
WWW: http://members.aol.com/BillSeurer

0 new messages