Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 25, 2009 5:42 AM
Hi,
I have a two dimensional array using c# (that is dynamic in size) that I need to be able to concatenate string from...sounds easy but what I need I just can't figure out how to do it. Here is an example of what I need to do:
The array might look like this -
array[0,0] = Test1
array[0,1] = Test2
array[1,0] = Temp1
array[1,1] = Temp2
array[1,2] = Temp3
array[2,0] = One
array[2,1] = Two
...and I need to concatenate the string in the array as follows...
Test1,Temp1,One
Test1,Temp1,Two
Test1,Temp2,One
Test1,Temp2,Two
Test1,Temp3,One
Test1,Temp3,Two
Test2,Temp1,One
...and so on...
I do not not the size of each dimension of the array, which raises my problem...I just cannot think of a way to do this.
If you could help or point me in the right direction, I would be very much appreciated!
Cheers,
Steve
All replies (10)
Friday, September 25, 2009 10:19 AM âś…Answered
Sounds to me that you want to generate all possible permutations of the array elements. Doing it correctly without generating duplicates is a bit tricky. It is done well in this project.
Hans Passant.
Friday, September 25, 2009 5:56 AM
GetLength() will get the dimension of the array. array.GetLength(0) gets the first dimension, array.GetLength(1) gets the second, etc.
Ron Whittle - If the post is helpful or answers your question, please mark it as such.
Friday, September 25, 2009 6:29 AM
Interesting problem. I was a lil free from my work so thought to do it. and here's the result of my rapid thought.
class MyStringConcatenation
{
static void Main(string[] args)
{
//string[][] a ={ new string[] { "0,0-of-a", "0,1-of-a"},
// new string[] { "1,0-of-a", "1,1-of-a"}
// };
//string[][] b ={ new string[] { "0,0-of-b", "0,1-of-b"},
// new string[] { "1,0-of-b", "1,1-of-b"}
// };
//string[][] c ={ new string[] { "0,0-of-c", "0,1-of-c"},
// new string[] { "1,0-of-c", "1,1-of-c"}
// };
string[][] a ={ new string[] { "0,0-of-a", "0,1-of-a", "0,2-of-a" },
new string[] { "1,0-of-a", "1,1-of-a", "1,2-of-a" },
new string[] { "2,0-of-a", "2,1-of-a", "2,2-of-a" }
};
string[][] b ={ new string[] { "0,0-of-b", "0,1-of-b", "0,2-of-b" },
new string[] { "1,0-of-b", "1,1-of-b", "1,2-of-b" },
new string[] { "2,0-of-b", "2,1-of-b", "2,2-of-b" }
};
string[][] c ={ new string[] { "0,0-of-c", "0,1-of-c", "0,2-of-c" },
new string[] { "1,0-of-c", "1,1-of-c", "1,2-of-c" },
new string[] { "2,0-of-c", "2,1-of-c", "2,2-of-c" }
};
int ai = 0, aj = 0, bi = 0, bj = 0, ci = 0, cj = 0;
for (; ai < a.GetLength(0); ai++)
{
for (; aj < a[ai].GetLength(0); aj++)
{
for (; bi < b.GetLength(0); bi++)
{
for (; bj < b[bi].GetLength(0); bj++)
{
for (; ci < c.GetLength(0); ci++)
{
for (; cj < c[ci].GetLength(0); cj++)
{
Console.WriteLine(a[ai][aj] + " # " + b[bi][bj] + " # " + c[ci][cj]);
}
cj = 0;
}
ci = 0;
}
bj = 0;
}
bi = 0;
}
aj = 0;
}
ai = 0;
Console.ReadLine();
}
}
Please mark as answer, if it helps you..
Thanks.
Friday, September 25, 2009 8:55 AM
@OP: Your sample array is not properly two dimensional. It is missing values for array[0,2] and array[2,2].
What do you intend to store in those missing elements? Just nulls, or what?
Friday, September 25, 2009 9:05 AM
Anyway, assuming that you have a regular 2D matrix, the following code will flatten it by column to an output that has a number of rows equal to the number of columns in the input matrix:
using System;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[,] matrix3x3 = new [,]
{
{"Test1", "Test2", "Test3"},
{"Temp1", "Temp2", "Temp3"},
{"One", "Two", "Three"}
};
Print(FlattenByColumn(matrix3x3));
string[,] matrix3x4 = new[,]
{
{"Test1", "Test2", "Test3", "Test4"},
{"Temp1", "Temp2", "Temp3", "Temp4"},
{"One", "Two", "Three", "Four" }
};
Print(FlattenByColumn(matrix3x4));
string[,] matrix4x3 = new[,]
{
{"Test1", "Test2", "Test3"},
{"Temp1", "Temp2", "Temp3"},
{"One", "Two", "Three"},
{"Red", "Green", "Blue" }
};
Print(FlattenByColumn(matrix4x3));
}
public static void Print(string[] array)
{
foreach (string s in array)
{
Console.WriteLine(s);
}
Console.WriteLine();
}
public static string[] FlattenByColumn(string[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
string[] result = new string[cols];
StringBuilder s = new StringBuilder();
for (int i = 0; i < cols; ++i)
{
s.Length = 0;
for (int j = 0; j < rows; ++j)
{
if (j > 0)
{
s.Append(", ");
}
s.Append(matrix[j,i]);
}
result[i] = s.ToString();
}
return result;
}
}
}
Friday, September 25, 2009 9:16 AM
@Hassan Mehmood:
Note that you are using string[][], but it looks like the OP was using string[,].
The OP needs to clarify what he meant, since the example he gave is ambiguous.
Friday, September 25, 2009 9:47 AM
@OP: Your sample array is not properly two dimensional. It is missing values for array[0,2] and array[2,2].
What do you intend to store in those missing elements? Just nulls, or what?
I don't understand. How Ii is missing values for array[0,2] and array[2,2]?
Friday, September 25, 2009 9:55 AM
@OP: Your sample array is not properly two dimensional. It is missing values for array[0,2] and array[2,2].
What do you intend to store in those missing elements? Just nulls, or what?
I don't understand. How Ii is missing values for array[0,2] and array[2,2]?
The OP showed us a 3x3 matrix (a total of 9 elements), but shows us only 7 values for it.
Therefore it is missing 2 elements. The two missing elements are at [0,2] and [2,2].
Note that the [,] notation is used for a multidimensional array, while the [][] notation is used for a ragged array.
Friday, September 25, 2009 9:55 AM
ok i got it. I thought it in some other way.
Tuesday, September 29, 2009 9:50 PM
Thanks Hans...this link really helped me to get it working. Much appreciated!
Steve