Extracting single dimensional array out of two dimensional array

M

Mukesh

Hi,
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?
Thanks
Chakravarti Mukesh
 
J

Jon Skeet [C# MVP]

Mukesh said:
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?

That's correct.

If you can produce a string[][] (an array of string arrays) instead,
it's a lot easier.

How much of what you want to do is fixed? Do you definitely need a
string[], or just (for example) an IEnumerable<string>?
 
K

Kalpesh

Having a jagged array can help in this.
string[][] strDetails

I think, foreach (string[]... ) should work in such a case.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mukesh said:
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?

For some options:

using System;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
int[,] a = new int[3,3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
a[i,j] = 3*i+j+1;
}
}
int[] a1 = new int[3];
Buffer.BlockCopy(a, 3*sizeof(int), a1, 0, 3*sizeof(int));
Console.WriteLine(a1[0] + " " + a1[1] + " " + a1[2]);
int[][] b = new int[3][];
for(int i = 0; i < 3; i++)
{
b = new int[3];
for(int j = 0; j < 3; j++)
{
b[j] = 3*i+j+1;
}
}
int[] b1 = b[1];
Console.WriteLine(b1[0] + " " + b1[1] + " " + b1[2]);
Console.ReadKey();
}
}
}

Arne
 
P

Peter Duniho

Mukesh said:
[...]
string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.

Well "strDetails" isn't an array of arrays; it's a single
two-dimensional array. So that's why you can't enumerate the "string[]"
instances in the "strDetails" array. There aren't any.

That said, there's nothing to stop you from iterating over a single
dimension of the two-dimensional array, and then within that iterating
over the other dimension for the purpose of building a new
single-dimensional array of strings.

Something like this:

string[,] strDetails;

for (int i = 0; i < strDetails.GetLength(0); i++)
{
string[] str = new string[strDetails.GetLength(1)];

for (int j = 0; j < str.Length; j++)
{
str[j] = strDetails[i, j];
}

// do something with str here
}

Pete
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Arne said:
Mukesh said:
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?

For some options:
...

Note that the block copy does not work for strings and therfore
is not applicable to your problem.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top