?
Solved

Vectors of Vectors???

Posted on 2003-03-16
17
Medium Priority
?
359 Views
Last Modified: 2010-03-31
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
Comment
Question by:Sphinx5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 160 total points
ID: 8146833
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8146836
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
 

Author Comment

by:Sphinx5
ID: 8146882
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 2

Expert Comment

by:functionpointer
ID: 8146907
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8146924
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 Comment

by:Sphinx5
ID: 8146926
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 Comment

by:Sphinx5
ID: 8146940
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
 
LVL 2

Expert Comment

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

Author Comment

by:Sphinx5
ID: 8146952
thank you fp! :D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8147001
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8147125
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
 

Author Comment

by:Sphinx5
ID: 8147155
Thank you both! :D Mind if I ask to split the points?
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8147181
split away as you see fit. :)
Curious, what did you come up with in the end?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8147214
OK
0
 

Author Comment

by:Sphinx5
ID: 8147236
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
 

Expert Comment

by:SpideyMod
ID: 8147567
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 Comment

by:Sphinx5
ID: 8147595
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question