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>
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>