C# 每个项目的计数(字节[]),并将其写入uint[]
我试图计算一个字节在我的字节数组中出现多少次,以便将其写入uint[],从而使我的输入正确。一个C# 每个项目的计数(字节[]),并将其写入uint[],c#,arrays,byte,uint,C#,Arrays,Byte,Uint,我试图计算一个字节在我的字节数组中出现多少次,以便将其写入uint[],从而使我的输入正确。一个byte[]arrayToConvert={97,98,99,97,98,99,97,98,100}其中写入ABCABD 我使用uint[]试图实现的是: 97 = 3 times 98 = 3 times 99 = 2 times 100 = 1 time 所以我试着在课堂上做到这一点: public static uint[] mCount(byte[] aCount) {
byte[]arrayToConvert={97,98,99,97,98,99,97,98,100}代码>其中写入ABCABD
我使用uint[]
试图实现的是:
97 = 3 times
98 = 3 times
99 = 2 times
100 = 1 time
所以我试着在课堂上做到这一点:
public static uint[] mCount(byte[] aCount)
{
for (int i = 0; i < aCount.Length; i++)
{
for (int j = i; j < aCount.Length; j++)
{
if (aCount[i] == aCount[j])
{
// somewhere arround here I think I must create the uint[] to return.
// but for this I would need to know howmany different bytes there are.
// not to forget I need to get my counter working to safe howmany of wich byte there are.
uint[] returncount = new uint[ !! number of different bytes !! ];
// foreach to fill the ^ array.
count = count + 1;
}
}
}
return returncount;
}
publicstaticuint[]mCount(字节[]a计数)
{
for(int i=0;i
所以在这一点上,我完全被卡住了。如果有人能把我推向正确的方向,那就太好了。或者告诉我在哪里可以读到这篇文章来更好地了解它。因为我似乎真的找不到一个我能理解的解释
提前谢谢,编码愉快 首先,您应该注意一个字节的范围是0到255
我认为最好的方法之一是声明一个大小为256的int(这里类型并不重要)数组,并将每个元素初始化为0
然后,只需迭代输入数组中的每个元素,将其用作新创建数组的索引并递增其值。
最后,int数组的每个元素都将包含其索引在输入上的出现
例如:
var aCount = new[] {97, 98, 99, 97, 98, 99, 97, 98, 100};
var occurrences = new int[256];
for (int i = 0; i < aCount.Length; i++)
{
var byteElement = aCount[i];
occurrences[byteElement]++;
}
for (int i = 0; i < occurrences.Length; i++)
if (occurrences[i] != 0)
Console.WriteLine($"{i} = {occurrences[i]} times");
var aCount=new[]{97,98,99,97,98,99,97,98,100};
变量出现次数=新整数[256];
for(int i=0;i
如果要返回每个字节存在的次数,为什么不返回字典
?至少使用它来获取计数,然后您可以从中仅返回值。还要注意的是,仅仅因为你不希望有负值,你就不应该使用uint
而不是int
aCount.GroupBy(b=>b.OrderBy(g=>g.Key)。选择(g=>$“{g.Key,3}={g.Count()}time{(g.Count()==1?”:“s”)”)
?@juharr正在摆弄代码。如果我能让它工作,我想做什么。就是将它们放入一个双链接列表中(我创建了一个数据结构)。。并创建一个分支-叶系统。我有我的想法,但去双链接列表。我需要一个数组。我想:P