Article View: borland.public.jbuilder.java.language
Article #20084Re: How work removeElementAt in java.util.Vector ?
From: Janne Johansson
Date: Fri, 31 Oct 1997 00:00
Date: Fri, 31 Oct 1997 00:00
56 lines
1951 bytes
1951 bytes
Marc Jourdenais wrote:
>
> The Vector indexes its elements from 0 to size-1, not 1 to size.
>
> - Marc
>
> Mok Chenda wrote:
>
> > I have the following code :
> >
> > Vector mCritereUtilisateur = new Vector();
> >
> > When i click on Button1 : i add string in my Vector
> > ...............
> > String vTemp;
> > vTemp = a value;
> > mCritereUtilisateur.addElement((String)vTemp);
> >
> > But when i want to delete a element, it's doesn't seem to work :
> >
> > Button 2 : removing the last element of my vector
> > .............................
> > int index = mCritereUtilisateur.size();
> > System.out.println("Numero de index = " + index);
> > if (index > 0) {
> > mCritereUtilisateur.removeElementAt(index);
> > }
> >
> > I have an error like :
> > Exception occurred during event dispatching:
> > java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
> >
> > Thanks for you're response
> > Mok Chenda
Bonjour Mok,
It seems that you have a off-by-one probleam. You see, the array starts
at index 0. In that case if you have n items in an array, the indexes of
the itemms are : 0, 1, 2... n-1. So, if you wish to remove the index use
int index = mCritereUtilisateur.size();
System.out.println("Numero de index = " + (index-1));
if (index > 0) {
mCritereUtilisateur.removeElementAt(index - 1);
}
instead!
That should work (I'm 99% sure).
Greetings,
Janne Johansson
Technical Details
Message-ID:
<3459DA3E.4313@datex-engstrom.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01bce561$f73dc460$1d4602c2@titan> <3458DE72.6A011FE6@pcn.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,5ebec6c16aaca00c X-Google-Attributes: gidf74b3,public From: Janne Johansson <janne.johansson@datex-engstrom.com> Subject: Re: How work removeElementAt in java.util.Vector ? Date: 1997/10/31 Message-ID: <3459DA3E.4313@datex-engstrom.com> X-Deja-AN: 286185853 References: <01bce561$f73dc460$1d4602c2@titan> <3458DE72.6A011FE6@pcn.com> Organization: Instrumentarium Oy, Datex-engstrom group Newsgroups: borland.public.jbuilder.java.language