Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 362

# Vectors of Vectors???

Hiya everybody,

I'm just totally confused over how to use this idea. This is what I'm wanting:

Pri1
Sec1    Sec2    Sec3    Sec4
Pri2
Sec1    Sec2    Sec3    Sec4    Sec5    Sec6
Pri3
Sec1    Sec2    Sec3

So the vector Pri1 has inside it a vector of 4 elements. Pri2 has a vector inside it of 6 elements etc...

I've tried something like the following:

final Vector l_vect_pri = new Vector(10,5);
Vector l_vect_sec;

l_vect_sec = new Vector(10,1);

for (int i = 0; i < l_vect_data.size() - 1; i++){
String data = (String)l_vect_data.elementAt(i);
}

return (l_vect_pri.size() - 1);
}

It is just a function that gets a vector passed to it. The function then splits up the vector as follows:

passed in:
a
b
c
d

after function

a
b    c    d

and if something else is passed in:

passed in:
zx
wda
tg

after function

a
b    c    d

zx
wda    tg

Something like that, am I going in the right direction? Also is the following correct:

To reference to pri2 vector I can use:

l_vect_pri.elementAt(1);

But how do I refer to the vector inside this???

Thank you
0
Sphinx5
• 7
• 5
• 4
• +1
1 Solution

Commented:
Think of the 'master' Vector as the table and individual Vectors within it as rows. Those rows contain values and these values are the elements contained in the row vectors. Let's say for the sake of argument that Sec1, Sec2 etc are strings:

Vector table = new Vector();
Vector pri1 = new Vector(); //create new row (it's empty)
// create some values
String sec1 = "A";
String sec2 = "B";
String sec3 = "C";
// add them to the row

// to access entire first row later
Vector pri1 = (Vector)table.get(0);

// then get sec2
String sec2 = (String)pri1.get(1); // gets sec2 = "B"
0

Commented:
After this:

>>
// add them to the row
>>

should come

to add the row to the table
0

Author Commented:
I don't fully understand that :S. It's more of the actual drawing of it. How would you put that down in drawing? (in the way that I planned it out in my original post).
0

Commented:
if you want to make this really easy, just remember, if a class wasn't made final, it was made to override.

why not:
public class PrimaryVector extends Vector{
public String toString() {
//go through any subVectors..
}
}

public class SecondaryVector extends Vector {
public String toString()
{
//toString and format the Vector elements...
}
}

this gives you alot more control over what each Vector does.  When you run into one in your code, you actually know whether its a pri or sec vector, but don't really have to care because hopefully you've overriden any functionality and it will behave accordingly. That's what OO is all about.  Not so we could worry about data types like a top/down language or write wrapper classes for all our objects.  ;-)
0

Commented:
Well, perhaps you could explain to us exactly what you're trying to do? To me, it looks like you're trying to create some type of tabular structure.
0

Author Commented:
could you elaborate a bit on that fp...I'm not a terribly good programmer :) I'm not fully sure what you mean by "if a class wasn't made final, it was made to override". That has just completely lost me! :)
0

Author Commented:
CEHJ, Its not that I don't fully understand it. Its just that I cna't visualise that solution like my one that I tried was:

pri1
sec1    sec2    sec3
pri2
sec1    sec2    sec3    sec4

etc...
0

Commented:
I'll defer this to CEHJ. I gotta go for a while. Dont worry. Your in excellent hands. :)
0

Author Commented:
thank you fp! :D
0

Commented:
Well, it's exactly like that ;-) i.e. pri1 is a vector (the first 'row') that contains three values: sec1, sec2 and sec3. Similarly with pri2.

pri1 and pri2 are themselves contained in another vector (the 'table') since they are conceptually rows of a table. Clear? Appropriate for what you want to do?
0

Commented:
sorry, had to get enough caffeine in my body to function...

try creating these two classes:

public class Table extends Vector{
private String name;
public Table( String name ) {
super();
this.name = name;
}

public String toString()
{
StringBuffer sb = new StringBuffer( "Table name: " + name );
for ( Enumeration e = elements(); e.hasMoreElements(); )
{
sb.append( "\n" );
sb.append(  e.nextElement() ); // Object.toString()
}

return sb.toString();
}
}

public class Row extends Vector {
private String name;
public Row( String name )
{
super();
this.name = name;
}
public String toString()
{
StringBuffer sb = new StringBuffer( "   Row:" + name );
for ( Enumeration e = elements(); e.hasMoreElements(); )
{
sb.append( e.nextElement() );
}
}
}

Now your code would do something like this:

Table table = new Table( "some table name" );
Row row1 = new Row( "some row name" ); //create new row (it's empty)

// add some values to the row

// to access entire first row later
Row r1 = (Row)table.firstElement();

// then get sec2
String sec2 = (String)r1.elementAt( 2 ); // gets sec2 = "B"

// to print the whole mess, simply
table.toString();

if you would like to eliminate the casting done when you access the table's rows, create a method in the Table class like:

public Row getRowAt( int i )
{
return (Row)elementAt( i );
}

-and/or-
public Row firstRow()
{
return (Row)firstElement();
}

and call these instead of   table.elementAt(i)  or table.firstElement(), respectively.

see?
0

Author Commented:
Thank you both! :D Mind if I ask to split the points?
0

Commented:
split away as you see fit. :)
Curious, what did you come up with in the end?
0

Commented:
OK
0

Author Commented:
Its a bit like the solution CEHJ gave. However I'm quite interested in yours, so when I have time I want to have a look over it. :-)
0

Commented:
I have reduced the points from 80 to 40 to facilitate a split per request here:http://www.experts-exchange.com/Community_Support/Q_20552642.html

Please remember to post back to the other expert with the URL for the points and include the question number or URL rom this question in the "points for" question.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange

0

Author Commented:
Thanks very much! :) Certainly helped me get my head around things...

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20552698.html

Thanks to both of you again,
Sphinx
0

## Featured Post

• 7
• 5
• 4
• +1
Tackle projects and never again get stuck behind a technical roadblock.