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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xoperatorCommented:
0
xoperatorCommented:
Tell me if you need anything else.
0
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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 RivkinFull 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
mikebirtCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.