C#代码问题

JS JS 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
斜体indices = new int[1]处,我不明白,为什么要new一个,
粗体indices=new int[count];我知道怎么回事,
但是斜体的代码块只用到indices[0]啊,刚接触不太懂,请高手指教


//函数Maxima
static int Maxima(int[] integers, out int[] indices)
{
Debug.WriteLine("Maximum values search started.");
indices=new int[1];
int maxVal=integers[0];
indices[0]=0;
int count = 1;
Debug.WriteLine(string.Format("Maximum values initialised to {0},at element index 0.", maxVal));
for (int i = 1; i < integers.Length; i++)
{
Debug.WriteLine(string.Format("Now looking at element at index{0}",i));
if (integers > maxVal)
{
maxVal = integers[i];
count = 1;
[i]indices = new int[1];

indices[0] = i;
Debug.WriteLine(string.Format("Now maximun found,Now value is {0},at element index{1}", maxVal, i));
}
else
{
if (integers[i] == maxVal)
{
count++;
int[] oldIndices = indices;
indices=new int[count];
oldIndices.CopyTo(indices,0);
indices[count-1]=i;
Debug.WriteLine(string.Format("Duplicate maximum found at element index {0}",i));
}
}
}
Trace.WriteLine(string.Format("Maximum value {0} found,with {1} occurrences",maxVal,count));
Debug.WriteLine("Maximum values search completed.");
return maxVal;
}

//主程序
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
int[] maxValIndices;
int maxVal = Maxima(testArray, out maxValIndices);
Console.WriteLine("Maximum value {0} found at element indices:",maxVal);
foreach (int index in maxValIndices)
{
Console.WriteLine(index);
}
Console.ReadKey();

...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
JS
C# 111,128 成员 642,536 内容