Using vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekbrave
    New Member
    • Dec 2007
    • 79

    Using vector

    Code:
    Vector v = new Vector();
    v=ctx.getAdminrole().getAttributeMultivalue();
    Now in the above code I am getting a set of strings in the object v.
    Now i want to use the strings one by one for comparison.
    So what type of code or loop i have to use to fetch the values stored in vector.

    Any pointers in this regard will be very helpful
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by abhishekbrave
    Code:
    Vector v = new Vector();
    v=ctx.getAdminrole().getAttributeMultivalue();
    Now in the above code I am getting a set of strings in the object v.
    Now i want to use the strings one by one for comparison.
    So what type of code or loop i have to use to fetch the values stored in vector.

    Any pointers in this regard will be very helpful
    There is no need to create a new Vector if you're retrieving an existing one in the
    next step. If you had used a generic vector you could've used the for-each loop:

    [code=java]
    Vector<String> v= ctx.getAdminrol e().getAttribut eMultivalue();
    ...
    for (String elem : v ) {
    // do something with an 'elem' from the vector
    }
    [/code]

    kind regards,

    Jos

    Comment

    • abhishekbrave
      New Member
      • Dec 2007
      • 79

      #3
      Originally posted by JosAH
      There is no need to create a new Vector if you're retrieving an existing one in the
      next step. If you had used a generic vector you could've used the for-each loop:

      [code=java]
      Vector<String> v= ctx.getAdminrol e().getAttribut eMultivalue();
      ...
      for (String elem : v ) {
      // do something with an 'elem' from the vector
      }
      [/code]

      kind regards,

      Jos
      [code=java]


      public class setsubrole extends BLTHAdapter{
      public void handleSetSubjec t(BLTHContext ctx) throws Exception
      {
      try{
      Vector v = new Vector();
      v=ctx.getAdminR ole().getAttrib uteMultiValue(" CUSTOM10");
      for(int i=0; i <= v.size();i++)

      {

      ctx.getAdminRol e().setAttribut eMultiValue("CU STOM06",(Vector ) v.elementAt(i) );

      }
      }

      catch(Exception e)
      {

      throw e;

      }

      }



      }
      [/code]

      This is my whole code. Intially in the vector v the values are string1 space string2 space string3
      Now I want to fetch string1 from the vector v.
      the error I am getting is java.lang.Strin g

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        A Vector holds Objects. Its access methods return Objects. Thus, you have to cast to String to call any String methods from entries in the Vector. Or, if you're using JDK > 1.5 (you should be), then you can use generics, eg a Vector<String>, to avoid all that.

        Comment

        Working...