Sign in to follow this  
jdub

What are my doing wrong? Java

Recommended Posts

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[i]);// 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.

Share this post


Link to post
Share on other sites
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[i]);// Exeption occurs here
}

vect.removeElement(3);

for (int i = vect.capacity()-1 ; i >= 0; i--){
System.out.print(vect.get(i));
}


}
}
[/source

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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[i]);
}


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this