简体   繁体   English

我应该使用什么数据结构?

[英]What data structure should I use?

Each of my object has these values: 2 short strings, 2 doubles and 5 TextBlocks (for a Windows app). 我的每个对象都有这些值:2个短字符串,2个双打和5个TextBlocks(对于Windows应用程序)。 I have about 30 of these objects. 我有大约30个这样的对象。

I will need to iterate through these objects regularly. 我需要定期迭代这些对象。 I have some ideas but want your independent suggestions for ease of iteration and selection of values from these objects. 我有一些想法,但想要您的独立建议,以便于迭代和从这些对象中选择值。

One consideration is that the size of the collection is static, it doesn't need to grow. 一个考虑因素是集合的大小是静态的,它不需要增长。

The properties in your object should not affect the type of collection you choose. 对象中的属性不应影响您选择的集合类型。 The key is the fact that the number of items in your collection remains static, so a list is overkill. 关键是您的集合中的项目数量保持不变,因此列表过度。 I would use a typed array like MyClass[]. 我会使用像MyClass []这样的类型化数组。 You can still use LINQ to query the array and filter by any public property. 您仍然可以使用LINQ查询数组并按任何公共属性进行过滤。

Here's a link to information about LINQ - LINQ . 这是一个有关LINQ - LINQ的信息的链接。 LINQ is what you're looking for. LINQ是您正在寻找的。 Consider LINQ statements like the following example. 考虑LINQ语句,如下例所示。 Here is the syntax of LINQ: 这是LINQ的语法:

var scores = new List<int>();
var Example =
        from score in scores
        where score > 80
        select score;

foreach (var example in Example) { doSomething(); }

The actual sorting only goes on when called at a later time. 实际排序仅在稍后调用时继续。 This is called deferred execution, one property of the example above. 这称为延迟执行,是上述示例的一个属性。 IE, Example's statement is only actually executed during: IE,Example的语句仅在以下期间实际执行:

 foreach (var example in Example) { doSomething(); }

There is also this to consider with LINQ: LINQ还要考虑这个问题:

Query syntax vs Method Syntax 查询语法与方法语法

I hope you have found this helpful. 我希望你发现这很有帮助。

Using Array of Objects........ 使用对象数组........

enter code here
MyClass
{
   public string str;

   public SetStr(string st)
   {
     str=st;
   }

   public GetStr()
   {
      return str;
   }

}

int main() 
{ 
     MyClass obj[30]; 
     int i; 
     for(i=0; i < 30; i++) 
       obj[i].str; //Access as required
     return 0; 
}

Also for get and Set properies can be used 也可以使用get和Set属性

List with 3 different classes or strings. 列出3个不同的类或字符串。 Generic class. 通用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用什么样的数据结构? - What kind of data structure should I use? 整数对应使用什么数据结构? - What data structure should I use for integer pairs? 我应该使用什么 C# 数据结构? - What C# Data Structure Should I Use? C#数据结构:应该使用哪种类型的集合? - C# data structure: What type of collection should I use? 我应该使用哪种数据结构来接受任何类型的对象并允许基于其不同属性进行分组? - What data structure should I use to accept any type of object and allow grouping based on its different properties? 我应该使用什么数据结构将三个“旧”值映射到三个“新”值? - What data structure(s) should I use to map three “old” values to three “new” values? 我应该使用什么数据结构来表示多对一映射? - What data-structure should I use to represent many-to-one mapping? 如何在C#中存储表-我应该使用哪种数据结构? - How to store tables in C# - What kind of data structure should I use? 我应该使用什么数据结构从可能的字符串数组中查找字符串? - What data structure should I use to look up a string from an array of possible strings? 我应该使用什么C#数据结构将不同数量的键/值对映射到字符串列表? - What C# data structure should I use to map a varying number of key/value pairs to a list of strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM