Results 1 to 6 of 6

Thread: A better alternative to a 2D string array?

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    A better alternative to a 2D string array?

    I need to lookup state postal abbreviation from state name. My impulse is to use a 2D array but so much of what I have read says arrays are deprecated and one should use string collections. But I don’t see how to have a multidimensional string collection. Is there a better solution than a 2D array?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: A better alternative to a 2D string array?

    Sounds like you may want to consider using a dictionary for this.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: A better alternative to a 2D string array?

    Arrays are far from deprecated. Arrays are all but essential. You should always use an array in a situation where they are appropriate because they are more efficient in such cases. In fact, collections often use an array internally. The main weakness of arrays is that they are fixed-size, which means that, once you create one, you cannot change the size of it. You can replace elements but you cannot add or remove them. There are a couple of options that make it appear that you can resize an array, e.g. ReDim and Array.Resize, but what they actually do is create a new array of the new size and copy the elements across. That becomes very inefficient if you do it repeatedly and/or for large arrays. If you need the ability to add and remove items then you should use a collection and if you don't then you should use an array.

    Your situation is a bit more complex than that. You should definitely not use a 2D array. A 2D should be used only where you want a matrix, i.e. where every element is a peer. For example, if you wanted to represent eggs in a tray. In your case, you're talking about one "column" representing one thing and the other "column" representing something else. Most definitely not peers. You could make a 2D array work, but it would be very inefficient. You would have to loop through the first column to find a match and that is much slower than using a Dictionary, where you simply pass in the key and it passes back the corresponding value..

  4. #4

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: A better alternative to a 2D string array?

    I had not learned dictionaries yet and now that you have pointed me in the right direction I can see it was exactly what I was looking for. In fact it is a collection but even better because it's designed for lookups. Thanks for to advice on arrays not being dead. Is there a list someplace with examples of when arrays should be used instead of collections?.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: A better alternative to a 2D string array?

    Quote Originally Posted by cory_jackson View Post
    Is there a list someplace with examples of when arrays should be used instead of collections?.
    An array is a simple list that cannot be resized. If that's all you need then that's what you use. If you need more than that then you use a collection of some sort. There are lots of different types of collection, e.g. List, Dictionary, Stack, Queue, HashSet, etc. Which one to use depends on the situation but the List(Of T) is the most commonly used because it is basically a dynamic array, i.e. a simple list that can be added to and removed from.

    As for multi-dimensional arrays, they have their place but they should only ever be used as a matrix. Whenever elements are not all peers, you should not use a multidimensional array. In those cases you should usually define a type to represent a single record and then create an array or collection of instances of that type.

  6. #6

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: A better alternative to a 2D string array?

    Good info. So much to learn. Thank you very much.

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