A simple Vector and generics question

Discussion in 'Java' started by Shawn, Nov 10, 2006.

  1. Shawn

    Shawn Guest

    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>
     
    Shawn, Nov 10, 2006
    #1
    1. Advertising

  2. On 10.11.2006 16:33, Shawn wrote:
    > 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
     
    Robert Klemme, Nov 10, 2006
    #2
    1. Advertising

  3. Shawn wrote:
    > 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
     
    Patricia Shanahan, Nov 10, 2006
    #3
  4. Shawn

    Shawn Guest

    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.
     
    Shawn, Nov 10, 2006
    #4
  5. Shawn wrote:
    > 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
     
    Patricia Shanahan, Nov 10, 2006
    #5
  6. Shawn

    Shawn Guest

    Patricia Shanahan wrote:
    >
    >> 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.
     
    Shawn, Nov 10, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    6,011
    John C. Bollinger
    May 20, 2005
  2. pmatos
    Replies:
    6
    Views:
    23,833
  3. Replies:
    8
    Views:
    1,932
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    571
    James Kanze
    Sep 4, 2007
  5. Soul
    Replies:
    0
    Views:
    527
Loading...

Share This Page