Public Group

What are my doing wrong? Java

This topic is 3917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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.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 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);// Exeption occurs here           }                      vect.removeElement(3);                      for (int i = vect.capacity()-1 ; i >= 0; i--){                System.out.print(vect.get(i));           }                          }}[/source

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 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 on other sites
Don't use the capacity() method of Vector. Use it's size() instead.

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);           }

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 on other sites
BTW "What am I" is spelled "What am I", not "What are my" (albeit they sound quite similar).

Share on other sites
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) ?

Share on other sites
Quote:
 Original post by opsVector(s) (deprecated in 1.4)

Really? I thought Vectors were synchronized ArrayLists.

Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by opsVector(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.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632591
• Total Posts
3007251

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!