Vectors

A

Andrew Farrington

Can anyone shed some light on a small problecm I am having. If you want a
load of integer vaiables you can just use an array like int[] myMarks =
new int[10];

This means you don't have to declare 1o seperate variables.

my problem is I want to use lots of vectors but i don't want to define
each one implicitly. I would like an array of vectors or a vector of
vectors.

Vector myVector = new Vector(); normal declaration

Vector[] myVector = new Vector[8]; this is accepted but when i reference
in my code later with

for (int i=0; i<8; i++)
{
myVector.add("index: " + i);
}

I keep getting null pointer exceptions.


I am trying to implement a tree structure, NOT binary. I would like a
root, then 8 children and then on each child, another 8 children. Vectors
is the way I thought I could do it but I am struggling. Anyone help? or
can you recommend a better solution (not JTree as I don't want a graphical
representation).

cheers

andy
 
C

Chris Smith

Andrew Farrington said:
Vector[] myVector = new Vector[8]; this is accepted but when i reference
in my code later with

for (int i=0; i<8; i++)
{
myVector.add("index: " + i);
}

I keep getting null pointer exceptions.


The line:

Vector[] myVector = new Vector[8];

does not create any Vector objects at all. Instead, it allocates an
array with enough space for 8 references to Vector objects. These
references initially don't point to anything at all; they are null. You
need to set them to something before you use them:

for (int i=0; i<8; i++)
{
myVector = new Vector();
myVector.add("index: " + i);
}

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Y

Yanick

Chris Smith said:
Andrew Farrington said:
Vector[] myVector = new Vector[8]; this is accepted but when i
reference
in my code later with

for (int i=0; i<8; i++)
{
myVector.add("index: " + i);
}

I keep getting null pointer exceptions.


The line:

Vector[] myVector = new Vector[8];

does not create any Vector objects at all. Instead, it allocates an
array with enough space for 8 references to Vector objects. These
references initially don't point to anything at all; they are null. You
need to set them to something before you use them:

for (int i=0; i<8; i++)
{
myVector = new Vector();
myVector.add("index: " + i);
}

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Personnally, if you're using vectors, and if you want flexibility, I suggest
you do this:

int n = 8;
Vector myVector = new Vector();

for (int i=0; i<n; i++)
{
myVector.add( new Vector() );
( (Vector)myVector.get(i) ).add( "index " + i );
}

-Yanick
 
A

Andrey Kuznetsov

Vector[] myVector = new Vector[8]; this is accepted but when i
reference
in my code later with

for (int i=0; i<8; i++)
{
myVector.add("index: " + i);
}

I keep getting null pointer exceptions.


The line:

Vector[] myVector = new Vector[8];

does not create any Vector objects at all. Instead, it allocates an
array with enough space for 8 references to Vector objects. These
references initially don't point to anything at all; they are null. You
need to set them to something before you use them:

for (int i=0; i<8; i++)
{
myVector = new Vector();
myVector.add("index: " + i);
}


see also http://jgui.imagero.com/doc/com/imagero/util/HashBag.html
it is Hashtable with Vectors.
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top