c# add 1 dynamic value to multi dimensional array

I  am reading a file with digits. I want to take every 2 digits and put them into a two dimensional array. I separated each digit with a ',' to easily make 1 large array, but I am not sure how to take each individual digit and put into the new two dimensional array.
123printAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mikebirtConnect With a Mentor Commented:
Hi,
It doesn't appear that you've accepted any of the answers you've been offered already, although they do appear to answer your original question. I guess all i would like to offer on this is; to address the dynamic nature you are asking for, a slight design thought and also to address a possible issue for your use of the previous code example.

Ok, firstly then. As everyone agrees, arrays are not dynamic. However, collections are. Why not use a collection? secondly, a multi dim array of associated data - this sounds to me like a lack of abstraction. for example, if these were x and y values, you could use the point class. Finally, in the previous example, it removes the empty entries. I figure this would deform your data and so i've left empties in but then not assumed i have a value... i only make that point as i've clearly based my code sample on the previous and it's a change i've made to it. Also, my approach doesn't assume you will always have an even number of values in your file. if you had the last value missing, that wold be fine.

If none of our answers are satisfactory to your problem then please respond with further details of what you're trying achieve.

HTH

Mike


        public class MyData
        {
            public int Value1 { get; set; }
            public int Value2 { get; set; }
        }

        private List<MyData> myDataList = new List<MyData>();

        private void ParseFile()
        {
            string[] rawItems = ReadFileContent().Split(new char[] { ',' });
            int value;

            for (int i = 0; i < rawItems.Length; i++)
            {
                if (!int.TryParse(rawItems[i], out value))
                {
                    value = int.MinValue;
                }

                if (i % 2 == 1)
                {
                    myDataList[myDataList.Count - 1].Value2 = value;
                }
                else
                {
                    myDataList.Add(new MyData() { Value1 = value});
                }
            }
        }
        private string ReadFileContent()
        {
            return "1,2,3,4,5,6,7,8,9,0";
        }

Open in new window

0
 
xoperatorCommented:
0
 
xoperatorCommented:
Tell me if you need anything else.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
123printAuthor Commented:
Yes. Ive seen this page many times. I know how to manually make and manipulate a multidimensional array but to dynamically add 1 value  at a time (due to the foreach loop taking data from the normal array) still seems to be little different to do.
0
 
xoperatorCommented:
You cant dynamically add values to the array. Array is a fixed data structure.
You can only assign value to it by accessing it with address.

like int[0] = 10;
or int[1] = 5;
0
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
first, if u plan to input the digits into 2D array why create 1 large array as u said?

assume this is your source file:
11,2,3,5

is this how the 2D array should built with those digits?

arr[0][0]=11
arr[0][1]=12
arr[1][0]=3
arr[1][1]=5

here's a sample code which does that.
the txt file i used contains the following: 1,2,3,4,5,6,7,8,9,0


string data = File.ReadAllText(@"c:\temp\1.txt");
            string[] tokens = data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[,] arr = new string[tokens.Length / 2, 2];

            for (int i = 0, j = 0; i < tokens.Length; j++, i += 2)
            {
                arr[j, 0] = tokens[i];
                arr[j, 1] = tokens[i + 1];
            }

Open in new window

0
All Courses

From novice to tech pro — start learning today.