467,910 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,910 developers. It's quick & easy.

Extracting single dimensional array out of two dimensional array

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

Oct 24 '07 #1
  • viewed: 3352
Share:
5 Replies
Having a jagged array can help in this.
string[][] strDetails

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

On Oct 24, 2:33 pm, Mukesh <cmukes...@gmail.comwrote:
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

Oct 24 '07 #2
Mukesh <cm*******@gmail.comwrote:
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>?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 24 '07 #3
Mukesh wrote:
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[i] = new int[3];
for(int j = 0; j < 3; j++)
{
b[i][j] = 3*i+j+1;
}
}
int[] b1 = b[1];
Console.WriteLine(b1[0] + " " + b1[1] + " " + b1[2]);
Console.ReadKey();
}
}
}

Arne
Oct 24 '07 #4
Mukesh wrote:
[...]
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
Oct 24 '07 #5
Arne Vajhøj wrote:
Mukesh wrote:
>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

Oct 24 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lecichy | last post: by
10 posts views Thread by Sona | last post: by
3 posts views Thread by Sim | last post: by
272 posts views Thread by Peter Olcott | last post: by
5 posts views Thread by nelly0 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.