Problems instantiating a Vector<Vector<Integer>>

Discussion in 'Java' started by the.real.doctor.zoidberg@gmail.com, Nov 1, 2006.

  1. Guest

    Hi,

    i'm having some problems with instantiating a vector of vectors of
    integers.
    >From the debugger i can see that all of the vector elements (which are

    vectors) are null. But i am unable to point them to a *new* vector
    because i can't iterate them.

    // i can create the "main" vector, but i still need to instantiate all
    100 Vectors of Integers
    Vector<Vector<Integer>> skips = new Vector<Vector<Integer>>(100);

    // the problem with this is that hasMoreElements returns false
    Enumeration it = skips.elements();
    if (it.hasMoreElements())
    elem = (Vector<Integer>)it.nextElement();

    // also suffers from the same problem... it says that it has no
    elements
    ListIterator<Vector<Integer>> it = skips.listIterator();
    if (it.hasNext())
    elem = new Vector<Integer>();


    How do i fix this?
    Thanks in advance!
     
    , Nov 1, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hi,
    >
    > i'm having some problems with instantiating a vector of vectors of
    > integers.
    >>From the debugger i can see that all of the vector elements (which are

    > vectors) are null. But i am unable to point them to a *new* vector
    > because i can't iterate them.
    >
    > // i can create the "main" vector, but i still need to instantiate all
    > 100 Vectors of Integers
    > Vector<Vector<Integer>> skips = new Vector<Vector<Integer>>(100);


    To begin with, Vectors are different from arrays.

    new Object[0] -- creates an array which can never have any elements
    new Object[100] -- creates an array with 100 elements, all of which are
    null to begin with

    new Vector(0) -- create a Vector with no space allocated for elements,
    but to which as many elements can be added as you like. It starts with
    none.
    new Vector(100) -- create a Vector with space allocated for 100
    elements, but to which as many elements can be added as you like. It starts
    with none.

    In either case, if you want to add elements to your new Vector, you can use
    the add() method (or create a ListIterator on the Vector and use its add()
    method.)

    >
    > // the problem with this is that hasMoreElements returns false


    Of course it does; the Vector has no elements.

    > // also suffers from the same problem... it says that it has no
    > elements


    Of course it does; the Vector has no elements.
     
    Mike Schilling, Nov 1, 2006
    #2
    1. Advertising

  3. hiwa Guest

    wrote:
    > Hi,
    >
    > i'm having some problems with instantiating a vector of vectors of
    > integers.
    > >From the debugger i can see that all of the vector elements (which are

    > vectors) are null. But i am unable to point them to a *new* vector
    > because i can't iterate them.
    >
    > // i can create the "main" vector, but i still need to instantiate all
    > 100 Vectors of Integers
    > Vector<Vector<Integer>> skips = new Vector<Vector<Integer>>(100);
    >
    > // the problem with this is that hasMoreElements returns false
    > Enumeration it = skips.elements();
    > if (it.hasMoreElements())
    > elem = (Vector<Integer>)it.nextElement();
    >
    > // also suffers from the same problem... it says that it has no
    > elements
    > ListIterator<Vector<Integer>> it = skips.listIterator();
    > if (it.hasNext())
    > elem = new Vector<Integer>();
    >
    >
    > How do i fix this?
    > Thanks in advance!

    If you haven't instantiated it, it should be null.
    It is only natural.
    Try this:
    -----------------------------------------------------------------
    import java.util.*;

    public class VecVec{

    public static void main(String[] args){
    Vector<Vector<Integer>> vvi;
    Vector<Integer> elem = null;

    vvi = new Vector<Vector<Integer>>();
    for (int i = 0; i < 100; ++i){
    vvi.add(new Vector<Integer>());
    }

    Enumeration<Vector<Integer>> it = vvi.elements();
    if (it.hasMoreElements()){
    elem = (Vector<Integer>)it.nextElement();
    }
    System.out.println(elem != null);

    elem = null;
    ListIterator<Vector<Integer>> lit = vvi.listIterator();
    if (lit.hasNext()){
    elem = (Vector<Integer>)lit.next();
    }
    System.out.println(elem != null);
    }
     
    hiwa, Nov 1, 2006
    #3
    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. pmatos
    Replies:
    6
    Views:
    24,029
  2. cayblood
    Replies:
    5
    Views:
    366
    cayblood
    Nov 3, 2005
  3. Replies:
    8
    Views:
    1,986
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    602
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    386
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page