到目前为止,我使用了多维数组int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6}};
,但现在我需要像list一样动态的东西。有多维列表这样的东西吗?我想创建一个像4列的表格。第4列应为10秒计时器,当10秒通过且特定行上的第2列和第3列未填满时,应从列表中删除整行。
答案 0 :(得分:7)
在.NET 4中,您可以创建tuples列表。
答案 1 :(得分:5)
尝试
class Foo{
public int Col1 { get;set; }
public int Col2 { get;set; }
public int Col3 { get;set; }
public int Col4 { get;set; }
}
和List<Foo>
答案 2 :(得分:2)
你想要的是List< List< yourtype > >
这会给你你想要的东西:
List< List<int> > lst = new List< List< int > >;
lst.Add( new List<int>() );
lst[0].Add( 23 );
lst[0][0] == 23;
答案 3 :(得分:2)
我建议你创建一个类,它封装你所说的列表中的单个“行”应该做的事情。它将为您的代码提供更具表现力和更具可读性的代码,这是任何优秀代码的核心特征。
所以最后你会得到List<YourSpecificClass>
答案 4 :(得分:1)
您可以使用包含以下列表的列表:
var multiList = new List<List<int>>();
再次阅读你的问题后,我认为这不是你想要的。我的猜测是你想要这样的东西:
public class Item
{
public int? Col1 { get; set; }
public int? Col2 { get; set; }
public int? Col3 { get; set; }
private Timer Timer { get; set; }
public List<Item> ParentList { get; set; }
public Item()
{
Timer = new Timer();
Timer.Callback += TimerCallBack();
// Set timer timeout
// start timer
}
public void AddToList(List<Item> parentList)
{
parentList.Add(this);
ParentList = parentList;
}
public void TimerCallBack()
{
if(!Col3.HasValue || !Col2.HasValue)
{
ParentList.Remove(this);
}
}
}
....
var list = new List<Item>();
var item = new Item { /*Set your properties */ };
item.AddToList(list);
这应该让你开始。您可以阅读Timer
here。
答案 5 :(得分:1)
这实际上取决于您的申请。例如,您可以使用List<List<int>>
,然后添加新项目:
myList.Add(new List<int> { 0, 0, 0, 0 });
从上次评论中可以看出,您将要对此列表中的项目应用特定逻辑,这表明您应该为项目创建一个类,例如
public class Item
{
public int Col1 { get; set; }
public int Col2 { get; set; }
public int Col3 { get; set; }
public int Col4 { get; set; }
public bool CanRemove { get { return Col2==0 && Col3 == 0 && Col4 == 0; } }
然后创建一个List<Item>
。然后您可以通过以下方式删除条目:
var toRemove = myList.Where(x => x.CanRemove).ToList();
foreach (Item it in toRemove)
{
myList.Remove(it);
}