C# 使用sortedList对列表中的单词进行计数
对于我的家庭作业,我必须使用分类列表来计算列表中的单词,分类列表将每个条目按字母顺序排序,然后再插入。在向用户显示数据时,显示的数据应按值排序,而不是按键排序 下面是我的尝试,但我得到了3个错误,我不知道如何解决它。我不允许为此使用LINQC# 使用sortedList对列表中的单词进行计数,c#,sortedlist,C#,Sortedlist,对于我的家庭作业,我必须使用分类列表来计算列表中的单词,分类列表将每个条目按字母顺序排序,然后再插入。在向用户显示数据时,显示的数据应按值排序,而不是按键排序 下面是我的尝试,但我得到了3个错误,我不知道如何解决它。我不允许为此使用LINQ List<string> words = new List<string>(); <--- Already populated List words=new List() 请修改此行并检查它是否工作。应该这样 Console.
List<string> words = new List<string>(); <--- Already populated
List words=new List() 请修改此行并检查它是否工作。应该这样
Console.WriteLine("\t{0}:\t{1}", tempSortList.GetKey(i), tempSortList.GetByIndex(i));
到
你在这里混淆了两件事。一个是SortedList(),另一个是SortedList()
GetKey和GetKeyList不在SortedList()中。您可以使用它代替GetKey
tempSortList.ElementAt(index); // This will return you a KeyValuePair.
对于第一个错误,您不能分配值keyvalPair。值只有getter。因此,不能通过执行+=1来设置其值
这不太好。需要一些改进,但它会起作用
for (int i = 0; i < d.Count; i++)
{
if (d.ElementAt(i).Key == word)
{
d.Values[i] += 1;
}
}
for(int i=0;i
或
for(int i=0;i
你从哪里复制了这段代码?@faisahafeez这是我自己的code@FaisalHafeez我从MSDN中发现了GetKey()方法,您混淆了两件事。一个是SortedList(),另一个是SortedList()。您正在使用第二个。它不包含GetKey和GetKeyList方法。如何设置它的值?我真的没办法了为什么要投否决票?无论如何,此行将执行值更新d[keyvalPair.Key]+=1的任务//或者任何你需要的价值
var key = tempSortedList.Keys[i];
var value = tempSortedList.Values[i];
Console.WriteLine("\t{0}:\t{1}", key, value);
tempSortList.ElementAt(index); // This will return you a KeyValuePair.
for (int i = 0; i < d.Count; i++)
{
if (d.ElementAt(i).Key == word)
{
d.Values[i] += 1;
}
}
for (int i = 0; i < d.Count; i++)
{
if (d.ElementAt(i).Key == word)
{
var val = d.ElementAt(i).Value + 1;
d.RemoveAt(i);
d.Add(word, val);
}
}