Are 2 Dimensional Lists Possible in C#

Are 2 dimensional Lists possible in c#?

Well you certainly can use a List<List<string>> where you'd then write:

List<string> track = new List<string>();
track.Add("2349");
track.Add("The Prime Time of Your Life");
// etc
matrix.Add(track);

But why would you do that instead of building your own class to represent a track, with Track ID, Name, Artist, Album, Play Count and Skip Count properties? Then just have a List<Track>.

Questions about 2D lists C#

Yes, you can indeed use multidimensional arrays or jagged arrays for storing "2D data".

As for creating a data structure that doesn't use any memory space for unused indexes, an option could be to use a dictionary where the keys are tuples of two numbers, like this (assuming that your data are strings):

var items = new Dictionary<(int, int), string>();

items.Add((0,1), "0-1"); //this throws an error if the key already exists
items[(2,3)] = "2-3"; //this silently replaces the value if the key already exists

Console.WriteLine(items.Keys.Contains((0,1))); //true
Console.WriteLine(items.Keys.Contains((0,2))); //false

Console.WriteLine(items[(2,3)]); //"2-3"

Of course you probably want to encapsulate this functionality in its own class, but you get the idea.

Note however that this dictionary approach will probably be worse than a plain array in terms of performance, but it's up to you to experiment and collect some metrics.

C# for loop of two dimensional List

You have 2 problems.

  1. When you first start looping over an inner loop you should initialize the loop in that place in the output collection.
  2. When you try to access [i][j] - to one that doesn't yet exist - which is everytime it will fail. Instead use the Insert method:

    List<List<double>> A = new List<List<double>>()
    {
    new List<double>() { 1, 0, 0 },
    new List<double>() { 1, -1, 0 }
    };

    List<List<double>> Temm = new List<List<double>>(A.Count);
    for (int i = 0; i < A.Count; i++)
    {
    Temm.Insert(i,new List<double>());
    for (int j = 0; j < A[i].Count; j++)
    {
    if (A[i][j] != 0) { Temm[i].Insert(j,A[i][j]); }
    else { Temm[i].Insert(j,Temm[i][j - 1]); }
    }
    }

Because each time you insert to the end of the list I would prefer to use the Add:

List<List<double>> B = new List<List<double>>();
for (int i = 0; i < A.Count; i++)
{
List<double> innerResult = new List<double>();
for (int j = 0; j < A[i].Count; j++)
{
if (A[i][j] != 0)
{
innerResult.Add(A[i][j]);
}
else
{
innerResult.Add(innerResult[j - 1]);
}
}
B.Add(innerResult);
}

Multidimensional List of ints C#

This example creates a list with a number of sublists of varying length and should serve as a good starting point for what you want to do.

List<List<int>> mainlist = new List<List<int>>();
List<int> counter = new List<int>() { 5, 4, 7, 2 };
int j = 0;

// Fill sublists
foreach(int c in counter)
{
mainlist.Add(new List<int>(c));
for(int i = 0; i < c; i++ )
mainlist[j].Add(i);
j++;
}

You could also add initialized lists to the main list

List<List<int>> mainlist = new List<List<int>>();
mainlist.Add(new List<int>() { 1, 5, 7 });
mainlist.Add(new List<int>() { 0, 2, 4, 6, 8 });
mainlist.Add(new List<int>() { 0, 0, 0 });

collection of two dimensional arrays in c#

If you want to use the multidimensional array you mentioned in your post

double[,]

then you can create a list of those objects

Like:

 List<double[,]> TwoDarray = new List<double[,]>();

Then to add one to the array you just use the list .add method

TwoDarray.add(new double[,]);

To access your new array you can simply use your

var x = TwoDarray[0];

2 dimensional array in C#

No (since C# array dimensions are fixed), but you could create an array of List<T>.

Can we have two dimensional generic list?

It's definitely possible. Whether it's a "good approach" or not depends on the problem you are trying to solve.

Getting a two dimensional array of all possible unique combination of numbers lower than a mixmum for each cell starting from 1

It's much easier to create jagged array (i.e. array of array), int[][] then 2D one int[,]:

using System.Linq;

...

private static IEnumerable<int[]> Solution(int[] maxes) {
if (null == maxes || maxes.Length <= 0 || maxes.Any(item => item < 1))
yield break; // Or throw exception(s)

int[] current = Enumerable
.Repeat(1, maxes.Length)
.ToArray();

do {
yield return current.ToArray(); // copy of current

for (int i = current.Length - 1; i >= 0; --i)
if (current[i] < maxes[i]) {
current[i] += 1;

break;
}
else
current[i] = 1;
}
while (!current.All(item => item == 1));
}

...

// Having an enumeration, we materialize it as an array, i.e. array of array
int[][] demo = Solution(new int[] { 2, 3, 2})
.ToArray();

// Let's have a look at the results
Console.Write(string.Join(Environment.NewLine,
demo.Select(line => string.Join(", ", line))));

Outcome:

1, 1, 1
1, 1, 2
1, 2, 1
1, 2, 2
1, 3, 1
1, 3, 2
2, 1, 1
2, 1, 2
2, 2, 1
2, 2, 2
2, 3, 1
2, 3, 2

If you insist on 2D array, you can convert:

  int[,] data2D = new int[demo.Length, demo.Length > 0 ? demo[0].Length : 0];

for (int y = 0; y < demo.Length; ++y)
for (int x = 0; x < demo[0].Length; ++x)
data2D[y, x] = demo[y][x];


Related Topics



Leave a reply



Submit