Solved

Save List<String[,]>

Posted on 2011-02-27
6
215 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
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 29

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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