Solved

Save List<String[,]>

Posted on 2011-02-27
6
217 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 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 Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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