using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ch07ex01
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 9, 3, 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();
}
static int Maxima(int[] integers,out int[] indices)
{
Debug.WriteLine("maximum value search started.");
indices = new int[1];\\这个数组是赋值吗?\\
int maxVal = integers[0];\\给maxVal的到底是什么\\
indices[0] = 0;
int count = 1;
Debug.WriteLine(string.Format("maximum value initialized to {0},at element index 0.", maxVal));
for (int i = 1; i < integers.Length; i++)\\ integers.Length这玩意长度去哪里知道?\\
{
Debug.WriteLine(string.Format("now looking at element at index{0}.", i));
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = 1;
indices = new int[1];
indices[0] = i;
Debug.WriteLine(string.Format("new maximum found.new 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 value search completed.");
return maxVal;\\为什么indices不用返回\\
}
}
}
不知道为什么书上结果是9 11,我这里是8 11,当然了,这不是主要的,主要的是代码一行一行的基本都读不懂= =,放假了,有无聊的大神前来解毒一下麽,新生只能看到这么多了,这段代码如果还有很值得加深印象的地方,也希望大神指出来
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ch07ex01
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 9, 3, 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();
}
static int Maxima(int[] integers,out int[] indices)
{
Debug.WriteLine("maximum value search started.");
indices = new int[1];\\这个数组是赋值吗?\\
int maxVal = integers[0];\\给maxVal的到底是什么\\
indices[0] = 0;
int count = 1;
Debug.WriteLine(string.Format("maximum value initialized to {0},at element index 0.", maxVal));
for (int i = 1; i < integers.Length; i++)\\ integers.Length这玩意长度去哪里知道?\\
{
Debug.WriteLine(string.Format("now looking at element at index{0}.", i));
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = 1;
indices = new int[1];
indices[0] = i;
Debug.WriteLine(string.Format("new maximum found.new 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 value search completed.");
return maxVal;\\为什么indices不用返回\\
}
}
}
不知道为什么书上结果是9 11,我这里是8 11,当然了,这不是主要的,主要的是代码一行一行的基本都读不懂= =,放假了,有无聊的大神前来解毒一下麽,新生只能看到这么多了,这段代码如果还有很值得加深印象的地方,也希望大神指出来