What are my doing wrong? Java

Started by
8 comments, last by OrangyTang 16 years, 3 months ago

public static void main(String args[]){
    
          Vector vect = new Vector(6,1);
         
           String names[] = {"Bob" , "Joe" , "Jorge" , "Cindy" , "Mary" , "Sally"};
           
           for (int i = names.length ; i >= 0 ; i-- )
           {
                vect.add(names);// Exeption occurs here
           }
           
           vect.removeElement(3);
           
           for (int i = vect.capacity() ; i >= 0; i--){
                System.out.print(vect.get(i));
           }
           
           
    }
  
   
    
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at VectorApp.Vect2.main(Vect2.java:18) Java Result: 1 What are my doing wrong? I've been looking at this for a while and I can't figure out? All help is appreciated. Thanks in advance.
J.W.
Advertisement
You are accessing out of bounds on the array of strings. The length of the array is 6 and you try and access element 6, which doesn't exist because the array is 0-indexed.

Try this:

public static void main(String args[]){              Vector vect = new Vector(6,1);                    String names[] = {"Bob" , "Joe" , "Jorge" , "Cindy" , "Mary" , "Sally"};                      for (int i = names.length - 1 ; i >= 0 ; i-- )           {                vect.add(names);// Exeption occurs here           }                      vect.removeElement(3);                      for (int i = vect.capacity()-1 ; i >= 0; i--){                System.out.print(vect.get(i));           }                          }}[/source
names.length evaluates to 6 since there are 6 elements in your "names" array, but the indexing operator [] expects a zero based index so only values between 0 to 5 (inclusive) are acceptable and any other indices outside this range generates an ArrayIndexOutOfBoundsException.
You don't need this manual indexing. If you have at least Java 5, try this:

import java.util.Collections;import java.util.Vector;class Foo{	public static void main(String args[])	{		Vector<String> vect = new Vector<String>();		Collections.addAll(vect, "Bob", "Joe", "Jorge", "Cindy", "Mary", "Sally");				vect.remove(3);		for (String name: vect)		{			System.out.println(name);		}	}}


BTW I changed removeElement to remove, since you do not want to remove the number three from your vector (which it does not contain), but the element at the index 3 (Cindy).
Don't use the capacity() method of Vector. Use it's size() instead.
You are trying to insert the elements in reverse order. Why?

This is the normal method of manual iteration:

           String names[] = {"Sally" , "Mary" , "Cindy" , "Jorge" , "Joe" , "Bob"}; // I reversed the array so you'll get the intended contents in the vector                      for (int i = 0; i < names.length; ++i)           {                vect.add(names);           }


Notice we start at zero and count up, and go up to strictly less than the length count. All these details make the code easier to (a) understand and (b) get right.

BUT, don't do it when you don't have to. Listen to DevFred.
BTW "What am I" is spelled "What am I", not "What are my" (albeit they sound quite similar).
BTW if you're going to use 1.6 syntax any reason not to refactor the code to use ArrayList(s) instead of Vector(s) (deprecated in 1.4) ?
Quote:Original post by ops
Vector(s) (deprecated in 1.4)

Really? I thought Vectors were synchronized ArrayLists.
Quote:Original post by DevFred
Quote:Original post by ops
Vector(s) (deprecated in 1.4)

Really? I thought Vectors were synchronized ArrayLists.

They are. And the 1.4 documentation states that if you want a syncronised ArrayList you should use:
List list = Collections.synchronizedList(new ArrayList(...));

IIRC Vectors aren't formally deprecated, but they're certainly discoraged.

This topic is closed to new replies.

Advertisement