Results 1 to 3 of 3

Thread: Array or Vector

  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Array or Vector

    I have to store 10 Strings. I can do that in Array or in Vector.

    Java Code:
    Vector <String> myValues = new Vector<String>(); 
    String [] myValues1 = new String[10];
    Just want to know, which one is the better choice. I am talking about memory and performance issues.

    Cheers

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    If you know how many items you need to store, I think an array is the way to go. The advantage of a vector is its ability to change size. Basically if a vector is full and you add another object, the memory for that object is dynamically allocated. This does not happen in an array. To increase the size of an array, you have to create a new one and copy all of the original values.

    So, in my opinion, if there is no chance that you will need more than 10 Strings, go with the array.

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks ShoeNinja for your opinion. I now have got the right idea.

Similar Threads

  1. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 03:31 AM
  2. How i add string array in vector
    By susan in forum New To Java
    Replies: 7
    Last Post: 02-12-2009, 02:32 PM
  3. Converting an Array to a Vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:44 PM
  4. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 08:33 PM
  5. array vs Vector
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 08: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
  •