Solved

Save List<String[,]>

Posted on 2011-02-27
6
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

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

Author Comment

by:dadadude
ID: 34993456
forget it
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 34993479
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:dadadude
ID: 34995284
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 75

Expert Comment

by:käµfm³d 👽
ID: 34999221
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
ID: 35005825
Question solved after all.
I need to grade it.

Thank You.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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