简体   繁体   English

什么时候应该使用数组列表(即列表) <string []> =新清单 <string []> ())在C#中?

[英]When should I use List of Array (i.e. List<string []> = new List<string []>()) in C#?

Context 上下文

I have two user input fields, and I want to save the input data into a List of String Array ( NOT in file or databse). 我有两个用户输入字段,我要输入的数据保存到字符串数组(而不是在文件或DATABSE)的列表。

As a part of my future work, I will have to perform search by specific value and list/display all the inputs that user has entered so far. 作为我未来工作的一部分,我将必须按特定值进行搜索,并列出/显示用户到目前为止输入的所有输入。 Moreover, the value of one input field should be associated with other input field. 此外,一个输入字段的值应与另一输入字段相关联。 For example- in the first field user writes "Nice", and second field he writes multiple words- "beautiful", "Excellent". 例如,在第一个字段中,用户写“ Nice”,在第二个字段中,他写多个单词:“ beautiful”,“ Excellent”。 That means the last two words should belong to the first one- "Nice". 这意味着最后两个单词应属于第一个单词“ Nice”。 Pretty much like a Synonym App . 几乎就像一个同义词应用程序

However, at this moment, my focus is to store the inputs, that happens when user clicks on Save button. 但是,目前,我的重点是存储输入,这是在用户单击“ Save按钮时发生的。

Code

Class that implements an Interface: 实现一个接口的类:

class SynonymApp : ISynonym
{
    private List <string[]> allItems = new List <string[]>();

    public void AddSynonyms(IEnumerable<string> synonyms)
    {
        //adding objects at the end of the list. Am I doing right?
        allItems.Add((synonyms.ToArray<string>()));
    }
}

Class where button click event happen: 发生按钮单击事件的类:

private void button1_Click(object sender, EventArgs e)
{
    List<string> userInput = new List<string>();
    string wordInput = textBox1.Text;
    if (wordInput == "") throw new Exception("Please give input");

    char[] emptySpace = new char [] {' ', '\t'};
    string[] synonymInput = textBox2.Text.Split(emptySpace);
    userInput.Add(wordInput);
    userInput.AddRange(synonymInput);
    synonymApp.AddSynonyms(userInput);
}

Questions 问题

  1. Is the right choice to save the string input into a List of Array over just a simple List or an array ? 将字符串输入保存到简单Listarray List是正确的选择吗? What does a List of Array do, in general? 通常, Array List有什么作用?
  2. Will my List of array (ie private List <string[]> allItems = new List <string[]>(); ) grow in size according to the needs? 我的数组列表(即private List <string[]> allItems = new List <string[]>(); )会根据需要增加大小吗?

If you really want the first word in the input to "own" the other two, I would use Dictionary<string, List<string>> . 如果您确实希望输入中的第一个单词“拥有”其他两个单词,则可以使用Dictionary<string, List<string>> That separates out the original word from the synonyms, and makes it searchable. 这样可以将原始单词与同义词分开,并使其可搜索。

You are correct that you need multidimensional storage (List of Array, Array of Array, etc.). 您正确的需要多维存储(数组列表,数组数组等)。 Or, better, create a SynonymEntry class: 或者,最好创建一个SynonymEntry类:

public class SynonymEntry
{
    public string EntryWord { get; set; }
    public List<string> Synonyms { get; set; }
}

Then you could create a simple List<SynonymEntry> to store all the data. 然后,您可以创建一个简单的List<SynonymEntry>来存储所有数据。

List vs. Array - I would only use an array if I know that there will never be new information added later. 列表与数组-仅当我知道以后再也不会添加新信息时,才使用数组。

Is the right choice to save the string input into a List of Array over just a simple List or an array? 将字符串输入保存到简单列表或数组中的正确选择是正确的选择吗? What does a List of Array do, in general? 通常,数组列表有什么作用?

In your case, No. Always try keep things simple. 就您而言,否。请始终尝试使事情简单。 I never see the use of List<string[]> . 我从来没有看到使用List<string[]> Arrays and List are two different things and should not be combined. 数组和列表是两个不同的东西,不应结合使用。 Even if you use it will be hard to maintain, to debug and won't be much optimal. 即使您使用它,也将难以维护,调试并且不会达到最佳状态。

Will my List of array (ie private List <string[]> allItems = new List <string[]>(); ) grow in size according to the needs? 我的数组列表(即private List <string[]> allItems = new List <string[]>(); )会根据需要增加大小吗?

Yes, but I would try to avoid using List<string[]> unless there is a specific reason. 是的,但是除非有特殊原因,否则我将尽量避免使用List<string[]>


IMO, you should organize your code into classes and use HashSet as it is usually faster than List . IMO,您应该将代码组织到类中并使用HashSet因为它通常比List快。

class MySynonyms
{
    public string Word { get; set; }
    public HashSet<string> Synonyms { get; set; }
}

Hope this helps. 希望这可以帮助。

You didn't tell the context of your application (you just said Pretty much like a Synonym App ), but if you really are storing synonyms, know that a synonym is a binary relation; 您没有告诉应用程序的上下文(您刚才说的很像Synonym App ),但是如果您确实要存储同义词,请知道同义词是二进制关系。 ie if A is synonymous to B, B too is a synonym of A. 即,如果A与B同义,则B也与A同义。

In this context, I'd just store all your words (whether main words, or synonyms) in a large list of strings ( List<string> ). 在这种情况下,我只是将您的所有单词(无论是主要单词还是同义词)存储在一个很大的字符串List<string>List<string> )中。 Alongside this list, I'd store another store the relationships in a List<(int ,int)> , storing the indexes of main word and synonym word in each Tuple. 除了这个列表,我还将另一个存储关系存储在List<(int ,int)> ,在每个元组中存储主要单词和同义词的索引。

If you use a DB to serialize this list (which you said you won't), This would translate to two simple tables; 如果使用数据库序列化此列表(您说不会),则这将转换为两个简单的表。 Words (int ID, varchar Word) and Relations (int ID1, int ID2). 单词(int ID,varchar Word)和关系(int ID1,int ID2)。

You may instead use List<List<string>> which would grow as needed. 您可以改用List <List <string >>,它会根据需要增长。 Another solution may be a Dictionnary<string,List<string>> You should try to choose the right one. 另一个解决方案可能是Dictionnary <string,List <string >>,您应该尝试选择正确的解决方案。 Array of string does not grow as easily as list. 字符串数组不像列表那样容易增长。

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

相关问题 如何从C#的列表中删除字符串数组? - How do I remove a String Array from a List in C#? 如何转换 C# 列表<string[]>到 Javascript 阵列?</string[]> - How do I convert a C# List<string[]> to a Javascript array? 如何在C#中将字符串分成数组(或列表)? - How do I break a string into an array (or List) in C#? c#我什么时候应该使用List,什么时候应该使用arraylist? - c# When should I use List and when should I use arraylist? 将Guid值分配给C#变量,即字符串 - Assign Guid value to C# variable i.e. string 如何在C#中通过String调用构造函数(即通过Reflection)? - How to invoke Constructors by String in C# (i.e. by Reflection)? c# - 我应该使用“ref”通过引用方法传递集合(例如List)吗? - c# - should I use “ref” to pass a collection (e.g. List) by reference to a method? C#添加列表 <string> 列表 <List<string> &gt;数组 - C# Add List<string> to List<List<string>> array 我可以从列表中获取元素吗 <T> NET中基于字符串的索引? (即:T val =列表 <T> [“ NameHere”]) - Can I get an element from a List<T> by string-based indexing in .Net? (I.E.: T val = List<T>[“NameHere”]) 使用linq检查字符串值是否在字符串数组中或C#中的List - Use linq to check if an string value is in string array or List in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM