Solved

Save List<String[,]>

Posted on 2011-02-27
6
213 Views
Last Modified: 2012-05-11
hello,
I have a List of 2D arrays.
for example as u can see in the code matrix contains a 2D array. And i want to save them in the file. The problem is that saving them is going by row not by Column!!!
How can i save them by column. these is something that i should change. But i can't figure it out.

Thank you.
StreamWriter data = new StreamWriter("caracteristiques.car");
        
        for (int i = 0; i < matrix.Count; i++)
        {
            for (int x = 0; x < matrix[i].GetLength(1); x++)
            {
                if (x > 0) data.WriteLine();
                for (int y = 0; y < matrix[i].GetLength(0); y++)
                {
                    string value = matrix[i][y, x];
                    if (y > 0) data.Write("    ");
                    data.Write(value);
                }
                
                
            }
        }
        data.Close();

Open in new window

0
Comment
Question by:dadadude
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
Again, what you mean "by column"? Your arrays have different number of elements in different directions. Any examples?
0
 

Author Comment

by:dadadude
Comment Utility
forget it
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
Comment Utility
OK, I put some example together - it may help you to clarify what is required.

The output:

A_0_0 A_1_0 A_2_0
A_0_1 A_1_1 A_2_1
A_0_2 A_1_2 A_2_2
---------------------------
B_0_0 B_1_0 B_2_0
B_0_1 B_1_1 B_2_1
B_0_2 B_1_2 B_2_2
B_0_3 B_1_3 B_2_3
---------------------------
C_0_0 C_1_0 C_2_0
C_0_1 C_1_1 C_2_1
C_0_2 C_1_2 C_2_2
C_0_3 C_1_3 C_2_3
C_0_4 C_1_4 C_2_4
---------------------------

You may still continue with deleting the question if you want...
private void button2_Click(object sender, EventArgs e)
        {
            // example
            string[,] ar1 = new string[3, 3];
            string[,] ar2 = new string[3, 4];
            string[,] ar3 = new string[3, 5];
            initialiseArray(ar1, "A");
            initialiseArray(ar2, "B");
            initialiseArray(ar3, "C");

            List<string[,]> matrix = new List<string[,]>();
            matrix.Add(ar1);
            matrix.Add(ar2);
            matrix.Add(ar3);

            saveMatrix(matrix);
        }

        private void initialiseArray(string[,] ar, string identifier)
        {
            // initialise
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < ar.GetLongLength(1); j++)
                {

                    ar[i, j] = identifier + "_" + i + "_" + j;
                }
            }
        }

        private void saveMatrix(List<string[,]> matrix)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string[,] ar in matrix)
            {
                for (int j  = 0; j < ar.GetLength(1) ;j++){
                    for (int i = 0; i < ar.GetLength(0); i++)
                    {
                        sb.Append(ar[i,j] + " ");


                    }
                    sb.AppendLine("");// line break
                }
                // array divider - we are saving number of arrays...
                sb.AppendLine("---------------------------");
            }

            using(StreamWriter outfile =  	new StreamWriter(@"C:\Test\arrays\arrays.txt")){
                outfile.Write(sb.ToString());
            }

        }

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dadadude
Comment Utility
hmmm it is still the same!!! it's not changing. as for the other question it's ok. It's a good answer
Thank you. I am trying to solve this question. Hope that it works.

thanks again.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
In the future, please provide as much relevant detail as you can. You understand your problem better than anyone else; clearly explaining it and responding to questions about it is the only way you'll ever get a clever answer.    = )
0
 

Author Comment

by:dadadude
Comment Utility
Question solved after all.
I need to grade it.

Thank You.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now