CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 1999
    Location
    India.
    Posts
    81

    are there any 2-d vectors?

    Hi,
    In java.util.Vector , one can only have one dimension, is there a method of having two dimensional methods.
    Thanks in advance
    Monika


  2. #2
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Re: are there any 2-d vectors?

    I've never tried this, but why not use a Vector of Vectors? Something along the lines of:

    Vector mainVector = new Vector();
    mainVector.addElement(new Vector());
    mainVector.addElement(new Vector());




    You could explicitly set the size of the vectors when you create them, if need be.

    "There's nothing more dangerous than a resourceful idiot." ---Dilbert
    BWAHAHAHAHAHAHA! ---Murray

  3. #3
    Join Date
    Jul 1999
    Location
    India.
    Posts
    81

    Re: are there any 2-d vectors?

    Hi,
    Thanks a lot for your reply. But vector of vectors won't store something like
    a[0][0]="hello";
    a[0][1]="index1";


  4. #4
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: are there any 2-d vectors?

    Hi
    You can do it like this:


    import java.util.*;

    class DoubleV{

    public static void main(String[] args){
    Vector outside = new Vector();
    Vector one = new Vector();
    Vector two = new Vector();
    one.add("Im the first Object inside the first Vector(one) inside the outside Vector");
    one.add("Im the second Object inside the first Vector(one) inside the outside Vector");
    two.add("Im the first Object inside the second Vector(two) inside the outside Vector");
    two.add("Im the second Object inside the second Vector(two) inside the outside Vector");
    outside.add(one);
    outside.add(two);
    for(int i = 0; i < outside.size(); i++){
    Vector v = (Vector)outside.elementAt(i);//cast back to Vector
    for(int j = 0; j< v.size(); j++)
    System.out.println((String)v.elementAt(j));//cast back to String
    }//end outside loop
    }//end main
    }



    You will have to cast back to the original type
    that you put into the Vector, in this case
    we put a Vector inside another Vector so
    we have to cast back to a Vector when we retrieve
    the element. The String also has to be cast back.
    The reason that we have to cast back is because the elementAt(int index) method
    returns an Object.
    Hope this helps
    Phill


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured