-2

I have a data in a following format:

{{{0}},{{1}},{{2,3}},{{1,2},{3,4}},{{5,6},{7,8},{9,10}},.... Is there any way of storing this in a jagged array? The data is large, and I would like to include it directly in the code. I searched internet and it says I can declare it in a following way:

{ new int[][] { new int[] {0} }, new int[][] { new int[] {1} }, 
    new int[][] { new int[] {2,3} }, new int[][] {new int[] {1,2} ,new int[] {3,4} } , ...

but typing those new int[][] would be too time consuming and I am looking for a way that I can used the original data directly into the code. Is there a way to do that? Any suggestion would be appreciated!

OmG
  • 18,337
  • 10
  • 57
  • 90
user109870
  • 13
  • 1
  • 3
  • 1
    I would be grateful if you can read up about the mark up and format the question so that it is readable. Thanks – Ed Heal Apr 01 '16 at 23:06
  • Thank you for pointing out that. Actually I am trying to make a windows forms application, in that case will the tag c# be appropriate? – user109870 Apr 01 '16 at 23:12
  • Depends on the programming language you wish to employ – Ed Heal Apr 01 '16 at 23:14
  • 1
    Well, tag it as C#, rather than wasting time of people who know C or C++ but not C#. The `new int` etc approach you have is not valid in C++ either. In C++, there is no direct way to do what you ask in a single declaration or statement, anyway. – Peter Apr 01 '16 at 23:15
  • Have you considered using loops? – siride Apr 01 '16 at 23:36
  • Everybody considers using loops. How exactly should I use loops? – user109870 Apr 01 '16 at 23:39
  • Is the depth of nesting limited to three or does it keep going on? The a tree structure might be better. What do the data describe? – roadrunner66 Apr 01 '16 at 23:40
  • Loops to initialize the arrays. You really should store this kind of stuff in a file or a database instead of in code. – siride Apr 02 '16 at 00:44

2 Answers2

1

From Jagged Arrays (C# Programming Guide), I think you can use mix jagged and multidimensional arrays to make it a little easier.

        int[][,] test = new int[][,] {
            new int[,] { { 0 } },
            new int[,] { { 1 } },
            new int[,] { { 2, 3 } },
            new int[,] { { 1, 2 }, { 3, 4 } }
        };
Tony
  • 135
  • 9
1

Given that your primary objective is to incorporate existing data into your code as an initialized c# jagged array, I suggest the following:

  1. Paste the existing data.
  2. Search and replace { with new [] {, except for the very first occurrence.
  3. Assign to a variable with type int[][][] (add additional []s if more than three levels).

Formatted for readability, the result should be a valid c# statement as follows:

int[][][] x =
{
    new[] {
        new[] {0}
    },
    new[] {
        new[] {1}
    },
    new[] {
        new[] {2,3},
        new[] {2,3}
    }
};
gxclarke
  • 1,953
  • 3
  • 21
  • 42