c# - Initiate jagged list -


i trying make jagged list. gets filled values depending on 2 variable int's: rows, , cols.

the pattern have list filled when rows = 4 , cols = 3:

00, 10, 20, 01, 11, 21, 02, 12, 22, 03, 13, 23 

each double digit sub-list containing column, , row. have:

namespace windowsformsapplication11 {     public partial class form1 : form     {         public form1()         {             initializecomponent();             definecellpositionslist();             displaycellpositionslist();         }          int rows = 4;         int cols = 3;          private list<list<int>> cellpositionslist = new list<list<int>>();          private void definecellpositionslist()         {             (int = 0; < (rows * cols); i++)             {                 list<int> sublist = new list<int>();                 (int row = 0; row < rows; row++)                 {                     (int col = 0; col < cols; col++)                     {                         sublist.add(col);                         sublist.add(row);                     }                 }                 cellpositionslist.add(sublist);             }         }          private void displaycellpositionslist()         {             (int = 0; < cellpositionslist.count; i++)             {                 label1.text += cellpositionslist[i][0];                 label1.text += cellpositionslist[i][1] + "\n";             }         }     } } 

the jagged list should have 12 sub-lists. sub-lists should have 2 values. working. each value 0. off logic. appreciated. thanks.

the problem not each of sublists { 0, 0 }, rather each 1 sublist of full 24 items, happens begin numbers 0, 0. can verify checking cellpositionslist[i].count.

the bug due looping when lists created. don't need 3 loops, 2 correct:

private void definecellpositionslist() {     (int row = 0; row < rows; row++)     {         (int col = 0; col < cols; col++)         {             cellpositionslist.add(new list<int> { col, row });         }     } } 

and there no need write of longhand, linq provides imho better alternative:

cellpositionslist = enumerable.range(0, rows)     .selectmany(r => enumerable.range(0, cols).select(c => new list<int> {c,r}))     .tolist(); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -