locked
save List<String[,]> to file RRS feed

  • Question

  • the problem is the following:
    I have the following List of 2D arrays:

    List<String[,]> matrix = new List<String[,]>();

    which is filled with 2D arrays. till now it's ok .
    2D Array of different length:
    so i want to loop through the list:
    Write the first 2D array array. Then go back to line 1. Write the second 2D array:
    For example:

    matrix 1 = {{1,2,3,4}, {5,6,7,8}}
    matrix2 =  {{1,2,3,4}, {5,6,7,8},{9,10,11,12}}

    matrix.Add(matrix1);
    matrix.Add(matrix2);

    Writing to file:

    i should have:

    1 5 1 5 9
    2 6 2 6 10
    3 7 3 7 11
    4 8 4 8 12

    that's basically it.

    Monday, February 28, 2011 11:41 AM

Answers

  • Hi,

    That's quite a nasty one. Here is a 80% solution, you need to fill in the other 20%. This is nasty code... or at least I don't like it, there will be a better way to do this.

     

                List<int[,]> matrix = new List<int[,]>();

                int[,] matrix1 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
                int[,] matrix2 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };

                matrix.Add(matrix1);
                matrix.Add(matrix2);

                for (int j = 0; j < 4; j++)
                {
                    foreach (int[,] entry in matrix)
                    {
                        for (int i = 0; i <= entry.GetUpperBound(0); i++)
                        {
                            Console.Out.Write(entry[i, j]);
                        }
                    }

                    Console.Out.Write("\n");
                }


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    • Marked as answer by hanito303 Monday, February 28, 2011 1:16 PM
    Monday, February 28, 2011 12:32 PM

All replies

  • Hi,

    That's quite a nasty one. Here is a 80% solution, you need to fill in the other 20%. This is nasty code... or at least I don't like it, there will be a better way to do this.

     

                List<int[,]> matrix = new List<int[,]>();

                int[,] matrix1 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
                int[,] matrix2 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };

                matrix.Add(matrix1);
                matrix.Add(matrix2);

                for (int j = 0; j < 4; j++)
                {
                    foreach (int[,] entry in matrix)
                    {
                        for (int i = 0; i <= entry.GetUpperBound(0); i++)
                        {
                            Console.Out.Write(entry[i, j]);
                        }
                    }

                    Console.Out.Write("\n");
                }


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    • Marked as answer by hanito303 Monday, February 28, 2011 1:16 PM
    Monday, February 28, 2011 12:32 PM
  • Thank you for your answer, It worked!!! lol Offffffffffff i tried like Many forums. 

    Woow yes it was a nasty one!! Thank you!! thank yoiu!!!! I passed most of my day trying to solve it.

     

    Thank you again!

    Monday, February 28, 2011 1:06 PM
  • Indeed it is hard. In python it's just one sentence. But Compared to the speed of c# its veryyyy slow. That's why i had to go back to c#.

     

    Laer when i master c# completely i'll strike back c++. it's beauty.

    Monday, February 28, 2011 1:20 PM
  • Your welcome hanito303, is that as in the Roland TB303?

    Didn't you post saying you had a solution? Doesn't matter. It was a little counter intuitive because the loop goes around the 2nd dimension first. The outer loop is usually the inner loop. Still can't help think that an easier solution is possible, an intermediate stage maybe were the list of matrix is flattened. Three loops like that ... don't like it. But it works so not so bad.

    Python is good but yeah at times slow. A balance between ease of coding and performance.


    Glad I could help. See you around.


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Tuesday, March 1, 2011 9:08 AM