How can I start initialization of "List of list" with columns
Solution 1
You need to create actual lists that you place inside your outer list of lists:
for (int i = 0; i < rowCount; i++)
{
Data.Add(new List<double>());
}
Once you've created all of the inner lists you can go through them and add values to them:
for (int i = 0; i < columnCount; i++)
{
for (int j = 0; j < rowCount; j++)
{
Data[j].Add((probabilityList[j]) * K);
}
}
Solution 2
You'll need to new
up each element of the List of Lists
Data = new List<List<double>>();
for (int i = 0; i < columnCount; i++)
{
List<double> column = new List<double>();
for (int j = 0; j < rowCount; j++)
{
//not really sure if this is the correct code here
//that would depend on your logic
column.Add((probabilityList[j]) * K);
}
Data.Add(column);
}
A list is not an array. It does not have slots that you just put elements in, it's more like a chain.
So initially you have a List<List<double>>
that is empty, it has no List<double>
instances in it. You have to (explicitly) create and add those lists to the Data
list.
Alternatively, if you do need row and columns with slots for elements, you could use a two-dimensional matrix, like this:
var matrixData = new double[rowCount, columnCount];
for (int i = 0; i < columnCount; i++)
{
for (int j = 0; j < rowCount; j++)
{
matrixData[j, i] = ((probabilityList[j]) * K);
}
}
Solution 3
Since you know the number of columns and rows you need you might think about using arrays instead of lists. When an array is initialized all members of the array are initialized to their default values. This would make your code
double[,] Data = new double[columnCount,rowCount];
for (int i = 0; i < columnCount - 1; i++)
{
for (int j = 0; j < rowCount - 1; j++)
{
Data[j,i] = (probabilityList[j]) * K);
}
}
Umut Koseali
Updated on June 04, 2022Comments
-
Umut Koseali about 2 years
I have a list of list and I want to add values column oriented. Like in the following code sample:
Data = new List<List<double>>(); for (int i = 0; i < columnCount; i++) { for (int j = 0; j < rowCount; j++) { Data[j][i] = (probabilityList[j]) * K); // It won't work } }
I know it won't work because Data's index does not exist at current situation, and I need to use add() method.
However, I need to add values to list starts with columns:
[1 , null] ----> [1, null] [null, null] [2, null]
How can I add values to a list of list starts with columns?
-
Umut Koseali over 11 yearsHi! This is not what I asked. This solution add values in row by row. I need to add values column by column (vertically).
-
Umut Koseali over 11 yearsHi! This is not what I asked. This solution add values in row by row. I need to add values column by column (vertically).
-
Servy over 11 years@UmutCanKöseali Trivial change. Just initialize all of the inner lists first, and then add items to each of those lists after they're created.
-
SWeko over 11 yearsThere are neither row not columns in a list of lists. I'll add code about using a matrix which does have rows and columns.
-
Umut Koseali over 11 yearsI want to tried to represent it visually for a better understanding. Thank you for your time I got my answer.