多维列表

发布于 2024-11-28 14:51:30 字数 197 浏览 0 评论 0 原文

到目前为止,我使用多维数组 int[,]numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6}}; 但现在我需要一些动态的东西,比如列表。是否存在多维列表之类的东西?我想创建一个类似 4 列的表格。第 4 列应该是 10 秒计时器,当 10 秒过去且特定行上的第 2 列和第 3 列未填满时,应从列表中删除整行。

Until now I used multidimensional arrays int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6}}; but now I need something dynamic like list. Is there such a thing as multidimensional lists? I want to create something like a table with 4 columns. The 4th column should be a 10sec timer and when the 10seconds pass and column 2 and 3 on the specific line are not filled the whole line should be deleted from the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

╭ゆ眷念 2024-12-05 14:51:30

在 .NET 4 中,您可以创建一个元组列表。

In .NET 4 you could create a list of tuples.

情泪▽动烟 2024-12-05 14:51:30

尝试

class Foo{
    public int Col1 { get;set; }
    public int Col2 { get;set; }
    public int Col3 { get;set; }
    public int Col4 { get;set; }
}

List

Try

class Foo{
    public int Col1 { get;set; }
    public int Col2 { get;set; }
    public int Col3 { get;set; }
    public int Col4 { get;set; }
}

and List<Foo>

不可一世的女人 2024-12-05 14:51:30

你想要的是 List<列表<你的类型> >

这会给你你想要的:

List< List<int> > lst = new List< List< int > >;
lst.Add( new List<int>() );
lst[0].Add( 23 );
lst[0][0] == 23;

what you want is List< List< yourtype > >

this will give you what you want:

List< List<int> > lst = new List< List< int > >;
lst.Add( new List<int>() );
lst[0].Add( 23 );
lst[0][0] == 23;
纸短情长 2024-12-05 14:51:30

我建议您创建一个类来封装您所说的列表中的单个“行”应该执行的操作。它将使您的代码更具表现力和可读性,这是任何优秀代码的核心特征。

所以最后你会得到 List

I suggest you create a class that encapsulates what you said a single "line" in your list should do. It will give your code that much more expressiveness and be far more readable, which is the core trait of any good code.

So in the end you will have List<YourSpecificClass>

坚持沉默 2024-12-05 14:51:30

这实际上取决于您的应用程序。例如,您可以使用 List>,并因此添加新项目:

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。然后您可以通过以下方式从中删除条目:

var toRemove = myList.Where(x => x.CanRemove).ToList();
foreach (Item it in toRemove)
{
   myList.Remove(it);
}

It really depends on your application. You could use List<List<int>>, for instance, and add new items thusly:

myList.Add(new List<int> { 0, 0, 0, 0 });

It sounds from your last comment that you're going to be applying specific logic to the items in this list, which suggests the possibility that you should create a class for your items, e.g.

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; } }

and then create a List<Item>. Then you can remove entries from it via:

var toRemove = myList.Where(x => x.CanRemove).ToList();
foreach (Item it in toRemove)
{
   myList.Remove(it);
}
时光瘦了 2024-12-05 14:51:30

您可以使用包含以下列表的列表:

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);

这应该可以让你开始。您可以在此处了解计时器< /a>.

You can just use a list that contains list like:

var multiList = new List<List<int>>();

After reading through your question again I don't think this is what you want. My guess is that you want something like this:

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);

That should get you started. You can read about the Timer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文