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

[Vector] How to setElementAt ?

4 views
Skip to first unread message

Julien BOURGEOIS

unread,
Jul 7, 1997, 3:00:00 AM7/7/97
to

Hello !

I can't figure out how to set an element in a
particular place using Vectors.
The standart method setElementAt seems to produce an out of
range error.

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.setElementAt(Vector.java)
at Noyau.ajoute_agent(Noyau.java:30)
at Noyau.main(Noyau.java:98)

I can't use addElement as it is told in the FAQ because
I have to set an element at a given index.

Any idea ?

Thanx,

Julien.

William Brogden

unread,
Jul 7, 1997, 3:00:00 AM7/7/97
to

I think you can only setElementAt( n ) when n is in the range of
objects already in the Vector, when you start there won't be any
so your first addition has to be an addElement();

WBB

Tiger Quimpo

unread,
Jul 9, 1997, 3:00:00 AM7/9/97
to

On 7 Jul 1997 12:34:28 GMT, bour...@esiee.fr (Julien BOURGEOIS)
wrote:

>Hello !
>
> I can't figure out how to set an element in a
> particular place using Vectors.
> The standart method setElementAt seems to produce an out of
> range error.
>
> java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
> at java.util.Vector.setElementAt(Vector.java)
> at Noyau.ajoute_agent(Noyau.java:30)
> at Noyau.main(Noyau.java:98)
>
> I can't use addElement as it is told in the FAQ because
> I have to set an element at a given index.

when you create a Vector, it is initially empty. even when you call
it with Vector(int) or Vector(int, int), the parameters just set some
optimization factors (so that less memory allocation and arraycopying
goes on). so you have to populate the vector first. if, at the point
of creating the Vector you don't have any idea what the values should
be, then you can populate it with nulls.

e.g.,

Vector v=new Vector();
for(vctr=0;vctr<VECTOR_LENGTH;vctr++)
v.addElement(null);

later you can set the correct values. of course, if you're going to
be accessing the data later and some of entries might not have been
set (i.e., they're still nulls), then you'll have to check for null
first before casting to some type and calling a method.

if you don't want to add elements in a loop as above (e.g., you might
not yet know what VECTOR_LENGTH should be), then you could write a
method to set the element. that method would check if the array is
already long enough. if it is, then it sets the appropriate element.
if it isn't, then it just adds nulls until it's long enough.

e.g.,

// Vector v is an instance object
public void setElementAt(Object element, int n)
{
while(v.elementCount < n)
v.addElement(null);

v.setElementAt(element,n);
}

if you're working on sparse arrays though (i.e., arrays where most of
the entries will be null, e.g., only 1/5th of the entries will be
non-null) then if the sparse array is large enough and sparse enough,
you probably shouldn't use Vectors. use something else, like some
sort of linked list or hashtable. unless you have a 64Meg machine and
everything fits in 64 Meg :). in which case, go for it :).

--------------------------------------------------------------

Gerald Timothy Quimpo entia non sunt multiplicanda praetere
ti...@kami.com necessitatem

mene sakhet ur-seveh

Anticipate charity by preventing poverty.
- Moses Maimonides,
Guide to the Perplexed

Roedy Green

unread,
Jul 9, 1997, 3:00:00 AM7/9/97
to

Julien BOURGEOIS wrote:
>I can't figure out how to set an element in a
> particular place using Vectors.
> The standart method setElementAt seems to produce an out of
> range error.

If you have a look at the source code, Vector will make a lot more sense.
setElementAt is for replacing an existing element. You need
InsertElementAt to add a new element.

You might find LinkedList of use too, a classic linked list implementation
of Vector, available free with source via my home page below.


Roedy Green Roedy rhymes with Cody ro...@bix.com
Canadian Mind Products contract programming (250) 285-2954
POB 707 Quathiaski Cove Quadra Island BC Canada V0P 1N0
http://oberon.ark.com/~roedy for CMP utilities and the Java glossary
-30-

0 new messages