I need a multidimensional data structure with a row and a column.
- Must be able to insert elements any location in the data structure. Example:
{A , B}I want to insertCin betweenAandB.{A, C, B}. - Dynamic: I do not know the size of the data structure.
- Another example: I know the [row][col] of where I want to insert the element. EX.
insert("A", 1, 5), whereAis the element to be inserted,1is the row,5is the column.
EDIT
I want to be able to insert like this.
static void Main(string[] args)
{
Program p = new Program();
List<List<string()>> list = new List<List<string>()>();
list.Insert("RAWR", 1, 2); // RAWR is the element to insert, 1 is the row, 2 is the col.
list.Insert("Hello", 3, 5);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
}
And of course this does not work, because the list does not support this functionality. I understand this code is bad, but I just want to get across what I am trying to accomplish.
So in a sense I will have a user who will choose which ROW and COL to insert the element to.
I think a list of lists should work fine:
You can insert new rows like this:
or insert a new element in a specific row:
Note that you need to have enough elements in your list in order to call an
Insert; otherwise, you will get an out-of-range exception. The easiest way to address this is to write a small utility method that adds empty items until the insertion is possible:Rewrite your program as follows: