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
  • Last Modified:

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;    
   
    public int addData(Vector l_vect_data){

        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);
            l_vect_sec.addElement(data);
        }
       
        l_vect_pri.addElement(l_vect_sec);            
       
        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
Asked:
Sphinx5
  • 7
  • 5
  • 4
  • +1
1 Solution
 
CEHJCommented:
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
pri1.add(sec1);
pri1.add(sec2);
pri1.add(sec3);

// 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
 
CEHJCommented:
After this:

>>
// add them to the row
pri1.add(sec1);
pri1.add(sec2);
pri1.add(sec3);
>>

should come

table.add(pri1);

to add the row to the table
0
 
Sphinx5Author 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
functionpointerCommented:
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
 
CEHJCommented:
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
 
Sphinx5Author 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
 
Sphinx5Author 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
 
functionpointerCommented:
I'll defer this to CEHJ. I gotta go for a while. Dont worry. Your in excellent hands. :)
0
 
Sphinx5Author Commented:
thank you fp! :D
0
 
CEHJCommented:
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
 
functionpointerCommented:
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
row1.add( "A" );
row1.add( "B" );
row1.add( "C" );

// 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
 
Sphinx5Author Commented:
Thank you both! :D Mind if I ask to split the points?
0
 
functionpointerCommented:
split away as you see fit. :)
Curious, what did you come up with in the end?
0
 
CEHJCommented:
OK
0
 
Sphinx5Author 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
 
SpideyModCommented:
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
 
Sphinx5Author Commented:
Thanks very much! :) Certainly helped me get my head around things...


FunctionPointer, please go:

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


Thanks to both of you again,
Sphinx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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