views:

481

answers:

3

I have a 2 dimensional array, like so:

char[,] str = new char[2,50];

Now, after I've stored contents in both str[0] and str[1], how do I store it in a

string[] s = new string[2];

?

I tried

s[0] = str[0].ToString();

but that seems to be an error: VC# expects 'two' indices within the braces, which means I can convert only a character from the array. Is there a way to convert the entire str[0] to a string? Or is changing it to a jagged array the only solution?

+2  A: 

Assuming the dimensions are fixed as 2x50:

char[,] str = new char[2,50];

// populate str somehow

// chose which of the strings we want (the 'row' index)
int strIndex = 0;
// create a temporary array (faster and less wasteful than using a StringBuilder)
char[] chars = new chars[50];
for (int i = 0; i < 50; i++)
  chars[i] = str[strIndex, i];
string s = new string[chars];

This would be easier and more performant if you used a jagged array:

char[][] str = new char[2][];

Then you could write:

string s = new string(characters[0]);
Drew Noakes
+3  A: 

A jagged array is almost always the best solution for a variety of reasons, and this is one good example. There is so much more flexibility available with an array of arrays than with a multi-dimensional array. In this case, once you have the values in an array of chars, then a constructor on the string class can be used to create a string from it.

Also, the jagged array would be composed of "vectors" (i.e., a single-dimensional arrays with a zero-lower-bound index), which are much more preferment in .Net because they are given special treatment by the CLR.

So without knowing what the rest of your program is doing, that would be my recommendation.

If you do attempt to construct a string manually by looping through the array indexes, instead of using a jagged array, then I recommend using the StringBuilder class to do it.

I just banged this out, but it should be something like this:

// For the multi-dimentional array
StringBuilder sb = new StringBuilder();
for (int stringIndex = 0; stringIndex < s.Length; stringIndex++)
{
  sb.Clear();
  for (int charIndex = 0; charIndex < str.UpperBound(1); charIndex++)
    sb.Append(str[stringIndex,charIndex]);
  s[stringIndex] = sb.ToString();
}

// For the jagged array
for (int index = 0; index < s.Length; index++)
  s[index] = new string(str[index]);
Jeffrey L Whitledge
Thanks... It's not required for me to use a rectangular array, so the use of StringBuilder is unnecessary complexity compared to simply switching to a jagged array. Based on this and your other points, I've chosen the latter solution. Thanks again.
sundar
+1  A: 

I would agree with using a jagged array. You can use this helper method to initialize a jagged array:

static T[][] InitJaggedArray<T>(int dimension1, int dimension2)
{
    T[][] array = new T[dimension1][];
    for (int i = 0; i < dimension1; i += 1)
    {
        array[i] = new T[dimension2];
    }
    return array;
}

So

char[,] str = new char[2,50];

would become

char[][] str = ArrayHelper.InitJaggedArray<char>(2, 50);

You would then access elements in it like so

str[0, 10] = 'a';

And to make it a string you would do

string s = new string(str[0]);
ICR