Results 1 to 5 of 5
  1. #1
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default Convert a vector to a string array

    Any easy way to conver this to a string array?:
    Java Code:
    Vector v = new Vector();
      v.add("convert"); 
      v.add("this");
      v.add("to");
      v.add("string"); 
      v.add("array");
    thx

  2. #2
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    try this:
    String[] ss = (String[])v.toArray(new String[v.size()]);

  3. #3
    peiceonly is offline Member
    Join Date
    Mar 2007
    Posts
    41
    Rep Power
    0

    Default

    I like coding too much :)
    Java Code:
    		Vector<String> v = new Vector<String>();
    		  v.add("convert"); 
    		  v.add("this");
    		  v.add("to");
    		  v.add("string"); 
    		  v.add("array");
    		  String[] strings =new String[v.size()];
              int i =0;
    		  for(String str : v){
    			  strings[i++] = str;
    		  }
    		  System.out.print(strings);

  4. #4
    dragon is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    Alternatively, the following solution might work, too:

    //Assume vector v already contains some Strings

    String[] st =new String[v.size()];
    v.toArray( st );

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dragon View Post
    Alternatively, the following solution might work, too:

    //Assume vector v already contains some Strings

    String[] st =new String[v.size()];
    v.toArray( st );
    This is incorrect as you never assign the array to anything.

    Plus it's already been answered correctly and more succinctly by Derrick, plus it's a 3 yr old thread. Because of the latter I'm locking it.

Similar Threads

  1. How i add string array in vector
    By susan in forum New To Java
    Replies: 7
    Last Post: 02-12-2009, 01:32 PM
  2. How to convert List to Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  3. Converting an Array to a Vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:44 PM
  4. Array or Vector
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 06:19 PM
  5. array vs Vector
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 07:07 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
  •