Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking for a smart way to fill a 2D array with an array. The 2D array is a grid with rows and cells. The input (a string) for the cells come from Console.ReadLine(). So I split the string to an string array and now I want to use this string array to fill the 2nd dimension of the data array.


C#
string[,] data = new string[height, width]; //grid

for (int i = 0; i < height; i++)
{
    string[] line = Console.ReadLine().ToCharArray().Select(c => c.ToString()).ToArray();
    
    data[i] = line; // <- compiler don't like this
}



Example:
the input string is "abc".

line looks like
line[0] == "a"
line[1] == "b"
line[2] == "c"

now I want to have this
data[0][0] == "a"
data[0][1] == "b"
data[0][2] == "c"

What I have tried:

Sure, I could use 'for', but in this case every piece of performance is important.
Posted
Updated 25-Sep-16 6:55am
v5
Comments
BillWoodruff 24-Sep-16 9:59am    
If you clarify your question, I'll respond with code. What we need to know here is:

do you:

1. want to make the value of a "cell" in a 2d array a 2d array

your2dary[0,0] returns an array ["a","b"]

or ...

2. are you wanting to just set the value of one cell by using a 2d array where the first value in the 2d array is the row index, and the second value is the column index.

Those are two different scenarios.

It is possible to create a Class that will allow an Array to be manipulated as in scneario two, but I advise you to forget about using a setter where the you are filling a 2d array, but only use one index in the setter: while it can be done, it's clever programming that is really bending the way arrays are supposed to be used, and will probably cause you problems in the future.

best wishes, Bil/l
Garth J Lancaster 24-Sep-16 23:58pm    
... probably better than my response in reality
Pierre Dole 25-Sep-16 6:20am    
What I looking for is the result:

data[0][0] == "a"
data[0][1] == "b"
BillWoodruff 25-Sep-16 9:08am    
At last you tell us what you really want to do, but you have not updated your original post. Support here is a two-way street. When you revise your original post so it is clear to anyone reading it, I'll respond with working code.
Pierre Dole 25-Sep-16 9:49am    
I updated the post. Sorry that I dont understand everything. My english is little poor. Thank you for you patient.

can you not use 'addRange' - Array.addRange Function[^] ?
 
Share this answer
 
Comments
BillWoodruff 24-Sep-16 14:19pm    
+4 That's a very good question to ask. Hopefully, the OP will clarify what their goal is here. Is it possible that what's needed here is a jagged array ?
Garth J Lancaster 24-Sep-16 23:56pm    
yes, I posted it and wasnt happy in that I'd supplied a pretty narrow answer, but then thought there'd be two outcomes, a) if the OP's requirements were for a jagged array surely they'd come back and say so, or b), as you did, someone else would question it
BillWoodruff 25-Sep-16 9:10am    
Hi Garth, well, the OP has revealed in a comment, at last, they are using a jagged Array, but, until they update their question, I'm not going to bother posting a response and working code.
I. Using an Extension Method: here we're just packaging up what you'd do with a for-loop:
C#
public static class ArrayExtensions
{
    public static void Array2dInsert<T>(this T[,] ary, int rowndx, int startcol, params T[] values)
    {
        if (ary.GetLength(0) < rowndx
            || ary.GetLength(1) < startcol)
        {
           throw new ArgumentOutOfRangeException("rowndx or startcol", "Error in Array2dInsert: Out of range row or column index");
        }

        if(values.Length > ary.GetLength(1) - startcol)
        {
           throw new ArgumentOutOfRangeException("values", "Error in Array2dInsert: Too many values");
        }

        foreach (var value in values)
        {
            ary[rowndx, startcol] = value;
            startcol++;
        }
    }
}
Test: execute this code in some Method or EventHandler
int[,] intary = new int[2,6];

intary.Array2dInsert(0,0,100,200,300);
Note that with this Extension Method you can specify the index in the "row" of the Array you wish to start inserting values at.

II. using a Class with custom Indexers:

I strongly suggest you study the following tutorials before you start reading this: [^],[^].

And, do review some of the many articles on Indexers here on CodeProject:[^].

Keep in mind that, internally in .NET, an Indexer is a Method.

First, let's look at what can be done, but, imho, should not be done:
C#
using System.Collections.Generic;

namespace ArrayUtilities
{
    public class Array2dEx<T>
    {
        private T[,] innerArray { set; get; }

        public Array2dEx(int sz1, int sz2)
        {
            innerArray = new T[sz1, sz2];
        }

        //  one value only indexer
        public T this[int ndx1, int ndx2]
        {
            get
            {
                return innerArray[ndx1, ndx2];
            }
            set
            {
                innerArray[ndx1, ndx2] = value;
            }
        }

        // multiple value indexer
        public  IEnumerable<T> this[int ndx1, params T[] args]
        {
            set
            {
                for (int i = 0; i < args.Length; i++)
                {
                    innerArray[ndx1, i] = args[i];
                }
            }
            
            get
            {
                for (int j = 0; j < innerArray.GetLength(1); j++)
                {
                    yield return innerArray[ndx1, j];
                }
            }
        }
    }
}
An example of using this Class:
C#
// in some Method or EventHandler

Array2dEx<string> test1 = new Array2dEx<string>(4, 5);

// put a break-point here

test1[0, 0] = "hello";
test1[0, 1] = "goodbye";

// note 1
string[] testary = new string[] { "what", "where" };
test1[2, testary] = null;

// note 2
var row2 = test1[2];

var row2item2 = test1[2, 1];
I suggest you copy the above code into a .NET project, and put a break-point at the place indicated and then step through the code, inspecting the values with each step.

The Class 'Array2dEx is a generic Class that holds a 2d Array of a Type you specify, and provides two different indexing methods. One indexing method is a replacement for the standard indexing method, and is exactly what you'd expect from a 2d Array index.

Note 1: the second indexing method uses the fact that a custom indexer can take arbitrary parameters, including, as shown in this case, a list of parameters of any size (using the 'params argument facility).

Note that the setter of the second Indexer does not make use of the 'value parameter that any Property provides; we used 'null as a "throw-away" argument. More about that, later: a setter must have some value passed to it when it's called.

Note 2: here the 'getter part of the indexer returns an IEnumerable<T>

Why you should not do this:

1. It's weird, "hackish," and bypasses the usual syntax for dealing with Arrays.

2. someone else in the future may look at the code and wonder what's going on with what appears to be assigning 'null to the unusual "stuff" between brackets.

... to be continued, tomorrow ... what you could use if you used a jagged Array, that I think is much better.
 
Share this answer
 
v9

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900