Home > Mobile >  how to create multidimensional arrays in C# without knowing the size
how to create multidimensional arrays in C# without knowing the size

Time:12-16

I need to understand on a practical level how to create a matrix[][] in C# without knowing the size. And consequently also how to modify it (delete elements depending on a search key).

I have an example loop. Two random string variables. Then I am no longer able to continue....

private static Random random = new Random();

for (int i=0; i<unKnown; i  ){
    var firstVar = RandomString(5);
    var secondVar = RandomString(20);

    //Matrix[][]
}


public static string RandomString(int length){
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
    .Select(s => s[random.Next(s.Length)]).ToArray());
}

Thank you

CodePudding user response:

Arrays are fixed size. They do not adjust their size automatically. E.g. the size is defined when creating the array with

string[] array = new string[10];

If your array is 2 dimensional (10x10) and you delete the value at (1:1) the Array still remains 10x10 but the field at 1:1 is null now.

If you need a solution that adjusts its size you might want to look into Lists.

Otherwise, I advise you to read the documentation.

CodePudding user response:

It really depend on what you want to do.

If you want a 2D array of values you can use multidimensional arrays. This supports arbitrary dimensions, but for more dimensions data sizes tend to go up and other solutions might be preferable:

var matrix = new double[4, 2];

If you want to do math you might want to use a library like Math.Net with specialized matrix types:

var matrix = Matrix<double>.Build.Dense(4, 2);

If you want to do computer graphics you likely want to use a specialized library, like system.Numerics.Matrix4x4

var matrix = new Matrix4x4();

It is also not particularly difficult to create your own matrix class that wraps a regular array. This has the benefit that interoperability is often easier, since most framework and tools accept accept pointers or 1D arrays, while few can handle a multidimensional array. Indexing can be done like:

    public class MyMatrix<T> 
    {

        public int Width { get; }
        public T[] Data { get; }
        public T this[int x, int y]
        {
            get => Data[y * Width   x];
            set => Data[y * Width   x] = value;
        }
     }

There is also jagged arrays, but there is no guarantee that these will be "square", so they are probably not appropriate if you want a "matrix".

In all cases you will need to loop over the matrix and check each element if you want to do any kind of replacement. Some alternatives require separate loops for width/height, while some allow for a single loop.

CodePudding user response:

I'm not sure if you want a matrix or an array.

Matrix would be like

string[,] matrix = new string[10, 10];

and array would be like

string[] array = new string[10];

You can access the array with array[i] and matrix with matrix[i, j]

You could also use List<List<string>> matrix = new List<List<string>>(); which may be more convenient to work with and can also be access with indexers. For example

matrix[i][j] = "bob";
matrix[i].RemoveAt(j);

Given the problem you have submitted maybe just a List<string> would work for you.

  • Related