Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problems instantiating a Vector<Vector<Integer>>

Reply
Thread Tools

Problems instantiating a Vector<Vector<Integer>>

 
 
the.real.doctor.zoidberg@gmail.com
Guest
Posts: n/a
 
      11-01-2006
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!

 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      11-01-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      11-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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);
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to in INSTANTIATING large number of components? nospam VHDL 4 06-06-2005 06:47 AM
Instantiating word in asp.net NM ASP .Net 1 12-22-2003 04:24 PM
Re: How to keep User Control from instantiating Mike Fors ASP .Net 2 08-27-2003 07:06 PM
Instantiating a class in.NET Carl ASP .Net 9 08-06-2003 05:24 AM
Re: ASP.NET Classlibrary not instantiating properly in legacy ASP pages Patrick Steele [MVP] ASP .Net 1 07-14-2003 09:18 PM



Advertisments