How To Store Mixed Array Data?

18,537

Solution 1

You may use object[] and do some type checking. You will get some boxing/unboxing when accessing the doubles, but that will be faster than double.Parse() anyway.

An alternative is to create a class with both types and a marker:

class StringOrDouble
{
    private double d;
    private string s;
    private bool isString;

    StringOrDouble(double d)
    {
        this.d = d;
        isString = false;
    }

    StringOrDouble(string s)
    {
        this.s = s;
        isString = true;
    }

    double D
    {
        get
        {
            if (isString)
                throw new InvalidOperationException("this is a string");
            return d;
        }
    }

    string S
    {
        get
        {
            if (!isString)
                throw new InvalidOperationException("this is a double");
            return s;
        }
    }
    bool IsString { get { return isString; } }
}

and then create an array of StringOrDouble. This will save you from some typechecking and boxing, but will have a larger memory overhead.

Solution 2

sure use:

object[], ArrayList, or List<Object>;

You'll have to cast, and probably pay a boxing penalty for the doubles, but they will allow you to store two different types.

Solution 3

Not the most efficient usage, but how about for flexibility:

var a = new object[] {1,2,3,"paul",5.5}

or even

var a = new object[] {1,2,3,"paul",5.5, new List<string>() {"apple","banana","pear"}  }

Solution 4

object[] and ArrayList are two decent options; it depends on whether you need a traditional fixed-length array or a vector (variable-length array). Further, I would "wrap" one of these in a custom class that did some type-checking, if strings and doubles are the ONLY things you can deal with, and you need to be able to get them back out as strings and doubles.

Share:
18,537

Related videos on Youtube

sooprise
Author by

sooprise

I like to program, but am a noob, which is why I'm here. &lt;-My sexy fatbooth picture

Updated on June 04, 2022

Comments

  • sooprise
    sooprise almost 2 years

    Let's say I have an array I need to store string values as well as double values. I know I can store the doubles as strings, and just deal with the conversions, but is it possible to use an array with two data types?

  • sooprise
    sooprise over 13 years
    Which would you recommend for maximum efficiency? I'm dealing with 50k element sized arays.
  • Justin Niessner
    Justin Niessner over 13 years
    @Soo - Is it always 50k elements and is each of the 50k elements always filled?
  • Richard J. Ross III
    Richard J. Ross III over 13 years
    I would think, that if you are needing to resize the array, that the arraylist or List<Object> would be better. If there is no resizing necessary, then just use an object[], as that will be better for fast lookup than the List<Object> or ArrayList.
  • kemiller2002
    kemiller2002 over 13 years
    50k...The three pretty much do the same thing, in terms of storage, but honestly, I would probably reconsider using an array at all and try and use several smaller structures.
  • KeithS
    KeithS over 13 years
    Array is generally fastest, since it doesn't have the variable-length overhead of ArrayList and List.
  • sooprise
    sooprise over 13 years
    ^ is what you are describing here a structure?