Extracting single dimensional array out of two dimensional array - CSharp

This is a discussion on Extracting single dimensional array out of two dimensional array - CSharp ; 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 ...

+ Reply to Thread
Results 1 to 6 of 6

Extracting single dimensional array out of two dimensional array

  1. Default 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


  2. Default Re: Extracting single dimensional array out of two dimensional array

    Mukesh <cmukesh19@gmail.com> 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?


    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 - <skeet@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

  3. Default Re: Extracting single dimensional array out of two dimensional array

    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.com> wrote:
    > 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




  4. Default Re: Extracting single dimensional array out of two dimensional array

    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

  5. Default Re: Extracting single dimensional array out of two dimensional array

    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

  6. Default Re: Extracting single dimensional array out of two dimensional array

    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


+ Reply to Thread

Similar Threads

  1. 1-dimensional array error
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 11-08-2007, 09:15 AM
  2. 2 dimensional array
    By Application Development in forum RUBY
    Replies: 7
    Last Post: 10-19-2007, 04:36 PM
  3. 3 dimensional array
    By Application Development in forum c++
    Replies: 8
    Last Post: 09-19-2007, 01:21 PM
  4. 2 dimensional array in struct
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 09-12-2006, 09:16 PM