A simple Vector and generics question

S

Shawn

Hi,

I am using Java 1.5. Usually I don't use generics. But my following
program won't compile. Error happens at "vec.setElementAt(new
Integer(4), 0); "

I am using Eclipse. After finishing writing the program, there is no
error. But when I want to run it, it won't. Error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at java.util.Vector.setElementAt(Vector.java:489)
at Demo.main(Demo.java:7)


Can you help me? Thank you very much


<code>
import java.util.*;

public class Demo {
public static void main(String[] args)
{
Vector vec = new Vector();
vec.setElementAt(new Integer(4), 0);
vec.setElementAt(new Double(4.4), 1);
vec.setElementAt("Hello", 2);

int num = (Integer)vec.elementAt(1);
double d = (Double)vec.elementAt(1);
String test = (String)vec.elementAt(2);

System.out.println("first =" + num);
System.out.println("second =" + d);
System.out.println("third =" + test);
}
}
</code>
 
R

Robert Klemme

Hi,

I am using Java 1.5. Usually I don't use generics. But my following
program won't compile. Error happens at "vec.setElementAt(new
Integer(4), 0); "

I am using Eclipse. After finishing writing the program, there is no
error. But when I want to run it, it won't. Error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at java.util.Vector.setElementAt(Vector.java:489)
at Demo.main(Demo.java:7)


Can you help me? Thank you very much


<code>
import java.util.*;

public class Demo {
public static void main(String[] args)
{
Vector vec = new Vector();
vec.setElementAt(new Integer(4), 0);
vec.setElementAt(new Double(4.4), 1);
vec.setElementAt("Hello", 2);

int num = (Integer)vec.elementAt(1);
double d = (Double)vec.elementAt(1);
String test = (String)vec.elementAt(2);

System.out.println("first =" + num);
System.out.println("second =" + d);
System.out.println("third =" + test);
}
}
</code>

This is not a problem related to generics but how you use the Vector.
You cannot set an element at a non existing position. (Hint: add
elements before you use setElementAt().)

Additional note: if you do not need thread safety (which you do not in
this case) an ArrayList is typically the better choice.

Kind regards

robert
 
P

Patricia Shanahan

Shawn said:
Hi,

I am using Java 1.5. Usually I don't use generics. But my following
program won't compile. Error happens at "vec.setElementAt(new
Integer(4), 0); "

I am using Eclipse. After finishing writing the program, there is no
error. But when I want to run it, it won't. Error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at java.util.Vector.setElementAt(Vector.java:489)
at Demo.main(Demo.java:7)


Can you help me? Thank you very much


<code>
import java.util.*;

public class Demo {
public static void main(String[] args)
{
Vector vec = new Vector();
vec.setElementAt(new Integer(4), 0);
vec.setElementAt(new Double(4.4), 1);
vec.setElementAt("Hello", 2);

int num = (Integer)vec.elementAt(1);
double d = (Double)vec.elementAt(1);
String test = (String)vec.elementAt(2);

System.out.println("first =" + num);
System.out.println("second =" + d);
System.out.println("third =" + test);
}
}
</code>

This has nothing to do with generics. Also, note that your program DOES
compile. If it didn't, there would be no Demo class file to run.

Generally, when you don't understand an exception from an API method,
the first thing to do is to look in the API documentation for the
method. The documentation for Vector's setElementAt says:

"The index must be a value greater than or equal to 0 and less than the
current size of the vector."

and

"Throws:
ArrayIndexOutOfBoundsException - if the index was invalid."

The size of your array is 0, and 0 is not less than 0, so the index is
invalid, and the exception you get is supposed to be thrown for that
condition.

You can't use setElementAt until after you have added some elements to
make the size non-zero.

Patricia
 
S

Shawn

Thank you all.

I still cannot understand the following:

vec.add(0, new Integer(4));
vec.add(1, new Double(4.4));
vec.add(2, "How are you?");
vec.add(5, "morning");

The last one is one error and the program cannot run. I purposely
skipped a couple positions.
There is a reason that I need skipping: I am "translating" Fortran code
to Java. In the original Fortran code, it takes an array several item
positions to hold a long string, like "How are you?" In Java, it only
needs one item position to hold it. I hope to keep index in Fortran and
Java correspondent, so I need skipping to let several item positions
unused. But the program will not run now.

Maybe I need:
vec.add(3, null);
vec.add(4, null);

?

Thank you very much.
 
P

Patricia Shanahan

Shawn said:
Thank you all.

I still cannot understand the following:

vec.add(0, new Integer(4));
vec.add(1, new Double(4.4));
vec.add(2, "How are you?");
vec.add(5, "morning");

The last one is one error and the program cannot run. I purposely
skipped a couple positions.
There is a reason that I need skipping: I am "translating" Fortran code
to Java. In the original Fortran code, it takes an array several item
positions to hold a long string, like "How are you?" In Java, it only
needs one item position to hold it. I hope to keep index in Fortran and
Java correspondent, so I need skipping to let several item positions
unused. But the program will not run now.

Maybe I need:
vec.add(3, null);
vec.add(4, null);

?

Thank you very much.

If you are translating Fortran code to Java, why are you using Vector?

Isn't a Java Object[] array a better match for a Fortran array. If you
want index alignment, the Java array can be dimensioned to match the
Fortran array. The only difference, references vs. values, also applies
to Java Vector.

All elements of a new Object[someLength] exist and are initialized null,
so there is no problem assigning to arbitrary elements.

But do remember that "dimension a(10)" creates an array with elements 1
through 10. "new Object[10]" has elements 0 through 9. You also have
this one element shift problem using Vector.

Patricia
 
S

Shawn

Patricia said:
Thank you very much.

If you are translating Fortran code to Java, why are you using Vector?

Isn't a Java Object[] array a better match for a Fortran array. If you
want index alignment, the Java array can be dimensioned to match the
Fortran array. The only difference, references vs. values, also applies
to Java Vector.

All elements of a new Object[someLength] exist and are initialized null,
so there is no problem assigning to arbitrary elements.

But do remember that "dimension a(10)" creates an array with elements 1
through 10. "new Object[10]" has elements 0 through 9. You also have
this one element shift problem using Vector.

Patricia

Great. Thank you very much.

By using Object array, the syntax is very similar now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top