Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在列表中找到最大值<;T>;在.NET2.0中?_C#_.net - Fatal编程技术网

C# 如何在列表中找到最大值<;T>;在.NET2.0中?

C# 如何在列表中找到最大值<;T>;在.NET2.0中?,c#,.net,C#,.net,我想从列表对象中找到最大值 public class SiteData { #region Fields private double? siteHeight; private double? siteWidth; #endregion #region Properties public double? Si

我想从列表对象中找到最大值

         public class SiteData
        {
             #region Fields
            private double? siteHeight;
            private double? siteWidth;

            #endregion



            #region Properties

            public double? SiteHeight
            {
                    get { return siteHeight; }
                    set { siteHeight = value; }
            }

            public double? SiteWidth
            {
                get { return siteWidth; }
                    set { siteWidth = value; }
            }
    }
现在我有了查找最大站点高度的函数

              public double FindMaxHeight(List<SiteData> objSite)
              {
               //logic to find the max. site height
               }
公共双FindMaxHeight(列表对象)
{
//查找最大场地高度的逻辑
}
我使用for循环来确定最大值,并面临性能问题。。。。 .Net framework 2.0-我现在正在使用


有人能告诉我如何在不使用for循环的情况下找到最大高度吗?可能吗?

您可以使用此最大值方法


如果这个方法在2.0.Net中没有出现,我认为最好用于

您可以使用这个Max方法


如果这个方法在2.0.Net中没有出现,我认为最好是用于

我会将对象添加到字典中,这样可以更快地按高度排序

对于所有经典容器,在插入速率和检索速率之间存在权衡

当更新很少时,此策略是有意义的,但您经常依赖于最大值

您需要重写GetHashCode()以返回高度较大的较小数字

我可以看出有一点困难,如果GetHashCode()是这样实现的,那么就会有重复的代码。您需要决定精度,而不是根据这一事实将重复项插入到集合中

或者,我会使用来自的多字典,然后依靠线性搜索模式来查找最上面箱子中的几个读数

internal int IndexForSortingBySiteHeight
{
   get
   {
      if(double.IsNaN(siteHeight) throw new ApplicationException();

      return (int)Math.Floor(siteHeight);
   }
}

public class ContainerForSortingBySiteHeight
{
    private List<SiteData> siteDataItems;

    public void Add(SiteData datum)
    {
        if(datum == null) return;

        siteDataItems[datum.IndexForSortingBySiteHeight] = datum;
    }

    public Max
    {
       get { return siteDataItems[0]; } // here's why a list won't work! What index should i use?
    }
}
内部int索引按站点高度排序
{
得到
{
if(double.IsNaN(siteHeight)抛出新的ApplicationException();
返回(整数)数学楼层(场地高度);
}
}
按站点高度运输的公共级集装箱
{
私有列表站点数据项;
公共无效添加(站点数据基准)
{
如果(基准==null)返回;
现场数据项【基准面指数按现场高度排序】=基准面;
}
公共最大值
{
get{return siteDataItems[0];}//以下是列表无法工作的原因!我应该使用什么索引?
}
}

我会将对象添加到字典中,这样可以更快地按高度排序

对于所有经典容器,在插入速率和检索速率之间存在权衡

当更新很少时,此策略是有意义的,但您经常依赖于最大值

您需要重写GetHashCode()以返回高度较大的较小数字

我可以看出,如果GetHashCode()以这种方式实现,则会有重复项。您需要确定精度,而不是根据这一事实将重复项插入到集合中

或者,我会使用来自的多字典,然后依靠线性搜索模式来查找最上面箱子中的几个读数

internal int IndexForSortingBySiteHeight
{
   get
   {
      if(double.IsNaN(siteHeight) throw new ApplicationException();

      return (int)Math.Floor(siteHeight);
   }
}

public class ContainerForSortingBySiteHeight
{
    private List<SiteData> siteDataItems;

    public void Add(SiteData datum)
    {
        if(datum == null) return;

        siteDataItems[datum.IndexForSortingBySiteHeight] = datum;
    }

    public Max
    {
       get { return siteDataItems[0]; } // here's why a list won't work! What index should i use?
    }
}
内部int索引按站点高度排序
{
得到
{
if(double.IsNaN(siteHeight)抛出新的ApplicationException();
返回(整数)数学楼层(场地高度);
}
}
按站点高度运输的公共级集装箱
{
私有列表站点数据项;
公共无效添加(站点数据基准)
{
如果(基准==null)返回;
现场数据项【基准面指数按现场高度排序】=基准面;
}
公共最大值
{
get{return siteDataItems[0];}//以下是列表无法工作的原因!我应该使用什么索引?
}
}

如果您对输入序列的顺序没有了解,那么没有循环是不可能的。
向我们展示您的最大值搜索代码,也许可以进行优化?

如果您没有关于输入序列顺序的知识,没有循环是不可能的。
向我们展示你的最大值搜索代码,也许优化是可能的?

这是一种相当奇怪的方法,但你可以用递归来实现

您将创建初始函数
double-FindMaxHeight(List-objSite)

这需要调用一个递归函数

int EnumerableLength(IEnumerable<T> enumerable)
{
    IEnumerator<T> enumerator = enumerable.GetEnumerator();
    return EnumeratorCount(enumertor, 0);
}

int EnumeratorCount(IEnumerator<T> enumerator, int count)
{
    if(enumerator.MoveNext())
    {
        count++;
        return EnumeratorCount(enumerator, count);
    }
    else
    {
        return count;
    }
}
int EnumerableLength(IEnumerable enumerable)
{
IEnumerator枚举器=可枚举的.GetEnumerator();
返回枚举器计数(枚举器,0);
}
int枚举器计数(IEnumerator枚举器,int计数)
{
if(枚举数.MoveNext())
{
计数++;
返回枚举器计数(枚举器,计数);
}
其他的
{
返回计数;
}
}

因此,您可以像这样在列表中移动,并将
(enumerator.Current作为SiteData)。SiteHeight
与当前获得的最大高度值进行比较,一旦到达列表末尾,您可以返回最大值。

这是一种非常奇怪的方法,但您可以使用递归实现

您将创建初始函数
double-FindMaxHeight(List-objSite)

这需要调用一个递归函数

int EnumerableLength(IEnumerable<T> enumerable)
{
    IEnumerator<T> enumerator = enumerable.GetEnumerator();
    return EnumeratorCount(enumertor, 0);
}

int EnumeratorCount(IEnumerator<T> enumerator, int count)
{
    if(enumerator.MoveNext())
    {
        count++;
        return EnumeratorCount(enumerator, count);
    }
    else
    {
        return count;
    }
}
int EnumerableLength(IEnumerable enumerable)
{
IEnumerator枚举器=可枚举的.GetEnumerator();
返回枚举器计数(枚举器,0);
}
int枚举器计数(IEnumerator枚举器,int计数)
{
if(枚举数.MoveNext())
{
计数++;
返回枚举器计数(枚举器,计数);
}
其他的
{
返回计数;
}
}

因此,您可以像这样浏览列表,并比较
(enumerator.Current作为SiteData).SiteHeight
设置为您当前获得的最大高度值,一旦到达列表末尾,您可以返回任何最大值。

正如GregC所说,优化取决于您的常见情况。如果列表是相当静态的,并且没有经常添加项,则可以在插入时对列表进行排序。这将从O(1)翻转插入排序算法的复杂性(有许多O(n logn)算法)和从O(n)到O(1)的检索

我宁愿把它列在一个清单上,而不是放在另一个清单上
public double FindMaxHeight(List<SiteData> objSite)
        {
            objSite.Sort(new Comparison<SiteData>((x, y) => y.SiteHeight.Value.CompareTo(x.SiteHeight.Value)));
            return  objSite.Count > 0 ?  objSite[0].SiteHeight.Value : 0;
        }
public  double FindMaxHeight(List<SiteData> objSite)
        {
            objSite.Sort(new Comparison<SiteData>((x, y)
                =>
                {
                    int xHeight, yHeight;
                    yHeight = y.SiteHeight.HasValue ? y.SiteHeight.Value : 0;
                    xHeight = x.SiteHeight.HasValue ? x.SiteHeight.Value : 0;
                    return yHeight.CompareTo(xHeight);
                }
            ));
            return  objSite.Count > 0 ?  objSite[0].SiteHeight.Value : 0;
        }