Tag: 列表

检查字符串列表中是否存在字符串数组

我有一个字符串数组列表。 我通过迭代创建一个新的字符串数组并尝试将其放入列表中,但是当我使用contains函数时它不会检查它是否存在,而是插入重复项。 List possibleColumnValues = new List(); while(true){ string[] rArr = new string[5]; //some code to populate the string goes here if (!possibleColumnValues.Contains(rArr){ { possibleColumnValues.Add(rArr); } }

如何从此输出中写入文本文件

我有以下代码,以便我可以搜索目录来查找文件。 现在我想为用户添加一种将输出保存到文本文件的方法? using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace RecursiveSearchCS { public class Form1 : System.Windows.Forms.Form { internal System.Windows.Forms.Button btnSearch; internal System.Windows.Forms.TextBox txtFile; internal System.Windows.Forms.Label lblFile; internal System.Windows.Forms.Label lblDirectory; internal System.Windows.Forms.ListBox lstFilesFound; internal System.Windows.Forms.ComboBox cboDirectory; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override […]

List.Add vs HashSet.Add for c#中的小集合

特定 HashSet set; List list; T t; 哪些对SMALL集合表现更好? if (! list.Contains (t)) list.Add (t); 要么 set.Add (t); 怀疑来源: HashSet vs. List性能

列表数组抛出NullReferenceException

我正在尝试制作一个列表列表。 我这样做: public static List[] words = new List[30]; public static List[] hints = new List[30]; 我称之为: foreach (string item in vars.directory) { reader2 = new StreamReader(item); while (reader2.Peek() > 0) { string line = reader2.ReadLine(); if (line.StartsWith(“#”)) { vars.words[counter].Add(line.Substring(1, line.Length – 1)); //here } else if (line.StartsWith(“-“)) { vars.hints[counter].Add(line.Substring(1, line.Length – 1)); //another here […]

单个排序的c#列表有不同的类型?

是的,我是c#的新手! :)我正在使用.Net4 VS2010。 我有三个类,每个类用于构建该类型的对象列表。 这三个都inheritance了基类。 我想将得到的三个列表合并为一个,并在其中一个基类元素上对它们进行排序。 这可以用不同类型的列表完成吗? 简化示例: 每个列表都已创建 public List TestOne list; public List TestTwoList; public List BothLists; 填写TestOne和TestTwo的代码…… 什么/如何将TestOne和TestTwo结合到BothLists中并在SeqNumber上对它们进行排序? public class BaseClassTest { public string Loc { get; set; } // loc // sequence number to order by will be assigned in the resulting class public int SeqNumber { get; set; } } […]

C#给定所需的订单需要空间有效的列表重新排序或排序

我的目标是:给定一个条目列表和一个所需的顺序,根据这个顺序重新排列条目列表。 该列表将非常大,因此空间效率非常重要。 例如: List data = ReadDataFromSomeWhere(); // data => [a, b, c]; List ordering = RandomPermutation(data.Count); // ordering => [2, 1, 3]; data.ReOrderBy(ordering); // data => [b, a, c]; 我可能会弄错,但似乎最直接和节省空间的解决方案是通过排序对数据进行排序/ 排序 。 或更一般地说: 给出两个列表:A,B有没有办法按B排序A? 该function基本上与以下相同: Array.Sort<(Of )>)(array[]()[], array[]()[]) 想到的一种方法是创建一个由A和B组成的新数据类型,即。 配对,然后按B值排序: List A; List B; Assert(A.Count == B.Count); var C = A.Select( (a,idx) => new […]

如何获取包含字符串的列表的索引

我有一个List ,我检查它是否包含一个字符串: if(list.Contains(tbItem.Text)) 如果这是真的,我这样做: int idx = list.IndexOf(tbItem.Text) 但是如果我有两个相同的字符串怎么办? 我想得到所有具有此字符串的索引,然后使用foreach循环遍历它。 我怎么能这样做?

使用Linq的列表顺序与sort不同

我想证实这一点,我试图使用Linq对我的class级列表进行排序。 但是当我使用排序函数时,似乎数据的顺序并没有以相同的方式排序。 假设列表包含4个ComputeItem,并且它们的所有A都设置为1,则所有B,C,D都设置为零。 情况1: ItemList = ItemList .OrderByDescending(m => mA) .ThenBy(m => mB) .ThenBy(m => mC) .ThenBy(m => mD) .ToList(); 与 案例2: ItemList.Sort( delegate(ComputeItem item1, ComputeItem item2) { if (item1.A == item2.A) { if (item1.B == item2.B) { if (item1.C == item2.C) { return item1.D – item2.D; } else { return item1.C – item2.C; } […]

列出从子到父的分配

我想这样做: List test = new List(); 我class的完整代码是这样的: class Program { public static void Main(string[] args) { List test = new List(); test.Add(new Child()); test.Add(new AnotherChild()); } } class Parent { } class Child : Parent { } class AnotherChild : Parent { } 有人可以告诉我为什么这给了我这个错误: 错误2无法将类型’System.Collections.Generic.List’转换为’System.Collections.Generic.List’d:\ personal \ documents \ visual studio 2010 \ Projects \ […]

如何按计数对列表进行排序?

在C#中: List<List> SectionList = new List<List>(); SectionList包含每个子列表在其包含的点数不同的点的列表。 我想弄清楚的是如何按子列表的计数按降序对SectionList进行排序。 因此,如果SectionList有3个点列表,则在排序后,SectionList [0]将包含所有3个列表的最高Count值。 谢谢,神话