Results 1 to 5 of 5
  1. #1
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default Adding elements to an Object Array

    Hi all,
    I have created an object array
    Java Code:
    Object elements[][] = {  {"first.doc","first.doc"},
                                   {"second.pdf","second.pdf"},      
                                   {"third.txt","third.txt"} 
              
     };
    Now, how will I create an array as shown above when the names("first.doc","second.pdf","third.txt") come inside a vector?

    The vector will be as given
    Vector v = new Vector();
    v.add("first.doc");
    v.add("second.txt");
    v.add("third.pdf");
    How will I create the array(by iterating through the vector with for loop)?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What you asking is not clear here. Do you want to add elements to a vector and iterate through that?

  3. #3
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What you asking is not clear here. Do you want to add elements to a vector and iterate through that?
    Please see how I created the array first
    Java Code:
    Object elements[][] = {  {"first.doc","first.doc"},
                                   {"second.pdf","second.pdf"},      
                                   {"third.txt","third.txt"} 
              
     };
    It was coded manually. Now that I have the data to create an array similar to the one above inside a vector.
    ie; v.elementAt(0) will be "first.doc" v.elementAt(1) will be "second.pdf" and so on.
    My question is, how to create the object array with the elements from this vector(using a for loop to add elements to the array from vector)

    Kindly say if my point is not clear.

  4. #4
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default

    Ok,
    I got it solved myself
    Object elements[][] = new Object[v.size()][2];
    for(int i =0; i<v.size(); i++)
    {
    elements[i][0] = v.elementAt(i).toString();
    elements[i][1] = mc.getIcone(mc.getExtension(v.elementAt(i).toStrin g().toUpperCase()));
    }

    Thanks for the help.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I don't know exactly what you are going to do. But seems to me you are trying to keep a record pair each time.

    Anyway, on the for loop you can still use the array length property.

Similar Threads

  1. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  2. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  3. Replies: 1
    Last Post: 12-14-2007, 07:25 AM
  4. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 02:19 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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