How to insert into a nested list - c#

I initialize a list of list:
List<List<double>> List = new List<List<double>>();
for(int k = 0; k < 4; k++)
{
liste_moyenne.Add(new List<double>(5));
}
I want to insert the values of a list (this list is generated by another function) like this:
for that I did this piece of code:
for (int i = 0; i < List.Count; i++)
{
for (int j = 0; j < myList.Count; j++)
{
List[i].Add(AddSmthToWork(myList[j]))
}
}
How can I do that?

Here's an example using the AddRange method:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var listOfList = new List<List<double>>();
for (var i = 0; i < 4; i++)
{
listOfList.Add(new List<double>(5));
}
foreach (var item in listOfList)
{
item.AddRange(GetList(5));
}
for(var i = 0; i < listOfList.Count; i++)
{
for (var j = 0; j < listOfList[i].Count; j++)
{
Console.WriteLine($"listOfList[{i}][{j}] = {listOfList[i][j]}");
}
}
}
private static IList<double> GetList(int maxCapacity)
{
var newList = new List<double>(maxCapacity);
for (var i = 0; i < maxCapacity; i++)
{
newList.Add(i);
}
return newList;
}
}
With the output:
listOfList[0][0] = 0
listOfList[0][1] = 1
listOfList[0][2] = 2
listOfList[0][3] = 3
listOfList[0][4] = 4
listOfList[1][0] = 0
listOfList[1][1] = 1
listOfList[1][2] = 2
listOfList[1][3] = 3
listOfList[1][4] = 4
listOfList[2][0] = 0
listOfList[2][1] = 1
listOfList[2][2] = 2
listOfList[2][3] = 3
listOfList[2][4] = 4
listOfList[3][0] = 0
listOfList[3][1] = 1
listOfList[3][2] = 2
listOfList[3][3] = 3
listOfList[3][4] = 4

See this tutorial
List<List<double>> nestedList = new List<List<double>>();
for (int i = 0; i < 4/*myCount*/; i++)
{
List<double> sublist = new List<double>();
for (int j = 0; j < 4/*myCount*/; j++)
{
sublist.Add(/*myValue*/)
}
nestedList.Add(sublist);
}

Related

Populate datagridview from database

I am trying to fill datagridview from database (cassandra), but for some reason it shows only one row instead of two, can you tell me what I am doing wrong.
public void show_db()
{
var cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
var session = cluster.Connect("hotel");
RowSet rs = session.Execute("SELECT * FROM hotel.staff");
foreach (var res in rs)
{
for (int i = 0; 2 < 3; i++)
{
for (int j = 0; j < 4; j++)
{
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("staff_id");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("username");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("password");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("name");
}
}
}
}
It's from database
This is from the application

I want to read a table from text file and assign each column to an array

i want to store each row in a different array. below is the code i tried.
but it doesn't not work, it only splits the last line and store values in "valueperline" array
first 11 rows are source text. file and screen shot of console
using System;
using System.Collections.Generic;
using System.IO;
namespace BBS_optimize
{
class Program
{
static void Main()
{
int i = 0; int j = 0; int k =0; string[] valueperline = new string[0]; string[] lines = new string [0];
lines = File.ReadAllLines("Table1.txt");
for (i = 0; i < lines.Length; i++)
{
Console.WriteLine(lines[i]);
}
for (j = 0; j<lines.Length; j++)
{ valueperline = lines[j].Split('\t');
}
for (k = 0; k < 44; k++)
{ Console.WriteLine(valueperline[k]);
}
}
}
}
Use array:
string[,] ParseFromFile (string fileName)
{
// Assume that your table have 10 cols and 100 rows
string[,] result = new string[10,100];
string str = File.ReadAllText (fileName);
// Split each line to arrays
string[] yourStringArray = str.Split(new[]{'\r', '\n'},StringSplitOptions.RemoveEmptyEntries);
for (int i == 0; i < yourStringArray; i++)
{
string[] row = yourStringArray[i].Split(new[]{" "},StringSplitOptions.RemoveEmptyEntries);
result[i] = row;
}
return result;
}
Use List:
List<List<string>> ParseFromFile (string fileName)
{
// Your list
List<List<string>> result = new List<List<string>>();
string str = File.ReadAllText (fileName);
// Split each line to arrays
string[] yourStringArray = str.Split(new[]{'\r', '\n'},StringSplitOptions.RemoveEmptyEntries);
for (int i == 0; i < yourStringArray; i++)
{
List<string> row = yourStringArray[i].Split(new[]{" "},StringSplitOptions.RemoveEmptyEntries).ToList();
result.Add(row);
}
return result;
}
below is a solution:
static void Main(string[] args)
{
List<List<string>> lst = new List<List<string>>();
string[] lines = new string[0];
lines = File.ReadAllLines("tableX.txt");
for (int i = 0; i < lines.Length; i++)
{
Console.WriteLine(lines[i]);
}
for (int j = 0; j < lines.Length; j++)
{
var line = lines[j].Split(' ').ToList();
lst.Add(line);
}
foreach (var item in lst)
{
for (int k = 0; k < item.Count; k++)
{
Console.WriteLine(item[k]);
}
}
}

cannot implicit convert type string string[]

I want to store string into string array, but it shows error.
Here is my code:
List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
string [] DomainArray;
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray = myresponse[i].EmailAddressSuffixes[j];
}
}
You are trying to assign the DomainArray (which is an array of strings) to a single string.
Try this, it adds all the values to a list then converts to list to an array:
List<ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainList = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainList.Add(myresponse[i].EmailAddressSuffixes[j]);
}
}
var DomainArray = DomainList.ToArray();
List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
var DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.Add( myresponse[i].EmailAddressSuffixes[j] );
}
}
Then you can get the array (if required) using DomainArray.ToArray()
Since arrays are not dynamic, think about working with lists:
List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.add(myresponse[i].EmailAddressSuffixes[j]);
}
}
I made two changes in my above code. first from #Hugo and second from #Roma
List <ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.Add(myresponse[i].EmailAddressSuffixes[j]);
}
}

Gather all possible combinations of 2 from a list

I have a list with 10 items in it. I am trying to output to console every possible pairing of 2. But it cannot pair with itself. For example
1,2
1,3
1,4 etc...
I found this to find all possible combinations within a list. Can someone help me modify it please?
private static void GetCombination(IList list)
{
var count = Math.Pow(2, list.Count);
for (var i = 1; i <= count - 1; i++)
{
var str = Convert.ToString(i, 2).PadLeft(list.Count, '0');
for (var j = 0; j < str.Length; j++)
{
if (str[j] == '1')
{
Console.Write(list[j]);
}
}
Console.WriteLine();
}
}
So if you have a list with 1 to 10, you need 1,2 1,3 1,4...1,10 - 2,1 2,3..2,10 and so on.
You just have to use bubble and check if the first index is different from the second.
For more clarification, here is an example:
List<int> mylist = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9 });
GetCombination(mylist);
private static void GetCombination(IList<int> values)
{
for (int i = 0; i < values.Count; i++)
{
for (int j = 0; j < values.Count; j++)
{
if (i != j)
{
Console.WriteLine(values[i] + " " + values[j]);
}
}
}
}
This is such a easy question.Adding to the answer of #FirstOne
you can also return the List containing all the combinations from the function:
List mylist = new List(new int[] { 1,2,3,4,5,6,7,8,9 });
GetCombination(mylist);
public static IList<Tuple<int,int>> GetCombination(IList<int> values)
{
List<Tuple<int,int>> _temp=new List<Tuple<int, int>>();
for (int i = 0; i < values.Count; i++)
{
for (int j = 0; j < values.Count; j++)
{
if (i != j)
{
_temp.Add(Tuple.Create(i, j));
}
}
}
return _temp;
}

Converting Codility solution to C# (Grocery-store, Hydrogenium 2013)

In order to learn and understand how Dijkstra's algorithm is used to solve the "Grocery Store" ([Hydrogenium 2013]: https://codility.com/programmers/challenges/hydrogenium2013) problem on codility, I'm trying to rewrite the #2, O(n^2) solution(https://codility.com/media/train/solution-grocery-store.pdf) in C#.
1) What language are those solutions written in?
2) What would be the C# equivalent to this bit of code?
G = [[]] *N
for i in xrange(M):
G[A[i]] = G[A[i]] + [(B[i], C[i])]
G[B[i]] = G[B[i]] + [(A[i], C[i])]
This is what I have so far
int[] G = new int[N];
for (int i = 0; i < M; i++)
{
G[A[i]] = G[A[i]];
G[B[i]] = G[B[i]];
}
Thanks in advance,
Gregory
Downloaded IDLE (a python IDE...kind of) and figured it out. It appears to be adding pairs to each array element. Here's the code I came up with if anyone else happens to stumble across the same problem.
private struct nodePair
{
public int node;
public int time;
public nodePair(int node, int time)
{
this.node = node;
this.time = time;
}
}
public int solution(int[] A, int[] B, int[] C, int[] D)
{
int M = A.Length;
int N = D.Length;
//build the graph
List<nodePair>[] G = new List<nodePair>[N];
for (int i = 0; i < N; i++)
{
G[i] = new List<nodePair>();
}
for (int i = 0; i < M; i++)
{
G[A[i]].Add(new nodePair(B[i], C[i]));
G[B[i]].Add(new nodePair(A[i], C[i]));
}
//initialize the distance table
int[] dist = new int[N];
for (int i = 0; i < N; i++)
{
dist[i] = int.MaxValue;
}
bool[] visited = new bool[N];
for (int i = 0; i < N; i++)
{
visited[i] = false;
}
//look for the minimum value
int ii = 0; ;
dist[0] = 0;
for (int k = 0; k < N; k++)
{
int s = int.MaxValue;
//find the minimum
for (int j = 0; j < N; j++)
{
if ((dist[j] < s) && (visited[j] == false))
{
s = dist[j];
ii = j;
}
}
visited[ii] = true;
if (s < D[ii])
{
return s;
}
List<nodePair> thisNodeLIst = G[ii];
foreach (nodePair oneNode in thisNodeLIst)
{
dist[oneNode.node] = Math.Min(dist[oneNode.node], s + oneNode.time);
}
}//for
return -1;
}
}

Categories

Resources