#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    91
    Rep Power
    13

    Vector of vectors


    My question is not a server side question as much as a general Java question about using a vector of vectors built up from parameters passed from an applet as follows...

    <applet
    code=Applet1.class
    name=Applet1
    width=400
    height=300 viewastext>

    <param name=item1 value="item1">
    <param name=item1sub1 value="item1 - sub1">
    <param name=item1sub2 value="item1 - sub2">
    <param name=item1sub3 value="item1 - sub3">

    <param name=item2 value="item2">
    <param name=item2sub1 value="item2 - sub1">
    <param name=item2sub2 value="item2 - sub2">

    <param name=item3 value="item3">
    <param name=item3sub1 value="item3 - sub1">

    </applet>

    ************************************************
    I can successfully build a vector of vectors however I am having problems accessing the nested vectors in the following java code...
    ************************************************

    import java.awt.*;
    import java.applet.*;
    import java.util.*;

    public class Applet1 extends Applet
    {
    Vector v1 = new Vector();
    int iItemCount = 1;

    public void init()
    {
    String sTemp;
    while(getParameter("item"+ iItemCount) != null)
    {
    Vector v2 = new Vector();

    int iSubItemCount = 1;
    while(getParameter("item"+ iItemCount +"sub"+ iSubItemCount) != null)
    {
    sTemp = getParameter("item"+ iItemCount +"sub"+ iSubItemCount);
    v2.addElement(sTemp);
    iSubItemCount++;
    }
    iSubItemCount = 1;

    v1.addElement(v2);
    iItemCount++;
    }
    iItemCount = 0;
    }

    public void paint(Graphics g)
    {
    for(int iCounter1 = 0; iCounter1 < v1.size(); iCounter1++)
    {
    /*** This displays the object as if it where some form of array ***/
    g.drawString("Vect"+ iCounter1 +" - "+ v1.elementAt(iCounter1).toString(),5,(iCounter1 * 15) + 20);
    /*** Does this show that the object is a vector? ***/
    g.drawString("Vect"+ iCounter1 +" - "+ v1.elementAt(iCounter1).getClass(),5,(iCounter1 * 15) + 80);
    }
    }
    }

    ************************************************

    When accessing the first vector (v1) it appears as though the object that is at any index of the v1 is in fact a vector from the getClass() method, however when I attempt to use it as such I get a "cannot convert Object to Vector" error message. I have tried everything including playing around with arrays but to no avail.

    Am I taking the totally wrong approach or have I missed something obvious?

    Thx
  2. #2
  3. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    13
    well i took a look at your code and i think i may know whats wrong..

    I am assuming that you are feeding objects of Type String into the Vector v2, and objects of Type Vector into V1

    to print it out you need to remember when getting elements from a vector you need to typecast, and i feel this is where your error is.

    for arguments sake try somethign like this and see if it work
    I am using System.out change this for your implementation.

    Code:
    for(int i=0; i<v1.size(); i++){
      Vector temp = (Vector)v1.elementAt(i);
      for(int c=0; c<temp.size(); c++){
        String curValue = (String)temp.elementAt(c);
        System.out.print(curValue + '\t');
      }
      System.out.println();
    }
    it should print out. You will notice that I typecast the vector from v1 as temp then typecast a string from temp.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    91
    Rep Power
    13

    Thumbs up AHA!


    Yes that has done the trick! I knew it would be something simple... thanks a million.

    Cheers

    grahowler
  6. #4
  7. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    13

    Thumbs up



IMN logo majestic logo threadwatch logo seochat tools logo