[英]Return Max value + name from a 2 dimensional List C#
我创建了一个二维列表,并希望返回最大值,最小值和具有与这些值相同的索引的名称。
这就是我所做的。
class Info
{
private string stad;
private double temperatur;
public string Stad
{
get { return stad; }
set { stad = value; }
}
public double Temperatur
{
get { return temperatur; }
set { temperatur = value; }
}
}
static double SearchMax(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
double Temperatur = double.MinValue;
foreach (Info temperatur in list)
{
if (temperatur.Temperatur > Temperatur)
{
Temperatur = temperatur.Temperatur;
}
}
return Temperatur;
}
static double SearchMin(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
double Tempertatur = double.MaxValue;
foreach (Info temperatur in list)
{
if (temperatur.Temperatur < Tempertatur)
{
Tempertatur = temperatur.Temperatur;
}
}
return Tempertatur;
}
但是我得到的只是最大值和最小值,而不是温度所在的城市的名称。
谁能告诉我或告诉我如何同时退货?
感谢您提供的所有帮助,但这只是让我回到原来的位置,然后再将其发布到此处。 我想要的是让程序同时返回城市和城市温度的信息,即“最温暖的城市是奥斯陆,温度为20°C”,而不是“最温暖的城市是consoleaplication.info”
编辑:2感谢您的所有帮助,尤其是Alex ^ _ ^
一个非常紧凑但效率低下的LINQ解决方案:
static Info SearchMin(List<Info> list)
{
return data.OrderBy(i => i.Temperatur).First();
}
但是您可能想修改自己的代码:
static Info SearchMin(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
double Tempertatur = double.MaxValue;
Info result = null;
foreach (Info temperatur in list)
{
if (temperatur.Temperatur < Tempertatur)
{
Tempertatur = temperatur.Temperatur;
result = temperatur;
}
}
return result;
}
如您所见,重点是返回一个Info
而不是double
。
您可以使用MoreLinq的MaxBy和MinBy方法来执行此操作。
public static partial class MoreEnumerable
{
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> selector)
{
return source.MaxBy(selector, Comparer<TKey>.Default);
}
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> selector, IComparer<TKey> comparer)
{
using (IEnumerator<TSource> sourceIterator = source.GetEnumerator())
{
if (!sourceIterator.MoveNext())
{
throw new InvalidOperationException("Sequence was empty");
}
TSource max = sourceIterator.Current;
TKey maxKey = selector(max);
while (sourceIterator.MoveNext())
{
TSource candidate = sourceIterator.Current;
TKey candidateProjected = selector(candidate);
if (comparer.Compare(candidateProjected, maxKey) > 0)
{
max = candidate;
maxKey = candidateProjected;
}
}
return max;
}
}
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> selector)
{
return source.MinBy(selector, Comparer<TKey>.Default);
}
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> selector, IComparer<TKey> comparer)
{
using (IEnumerator<TSource> sourceIterator = source.GetEnumerator())
{
if (!sourceIterator.MoveNext())
{
throw new InvalidOperationException("Sequence was empty");
}
TSource min = sourceIterator.Current;
TKey minKey = selector(min);
while (sourceIterator.MoveNext())
{
TSource candidate = sourceIterator.Current;
TKey candidateProjected = selector(candidate);
if (comparer.Compare(candidateProjected, minKey) < 0)
{
min = candidate;
minKey = candidateProjected;
}
}
return min;
}
}
}
(编辑以删除注释和对参数进行空检查。)
一旦有了它,这将变得非常简单:
Info maxInfo = list.MaxBy(item => item.Temperatur);
Info minInfo = list.MinBy(item => item.Temperatur);
确保可以对其进行一些调整以使其更短:
var maxtemp = list.Max(x => x.Temperatur);
var maxstad = list.First(x => x.Temperatur == maxtemp).Stad;
var mintemp = list.Min(x => x.Temperatur);
var minstad = list.First(x => x.Temperatur == mintemp).Stad;
您可以按Temperatur
订购,然后取第一个。
在大名单上这可能很慢
static Info SearchMax(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
return list.OrderByDescending(i => i.Temperatur).FirstOrDefault();
}
static Info SearchMin(List<Info> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Tomt Lista");
}
return list.OrderBy(i => i.Temperatur).FirstOrDefault();;
}
private static TempratureInfo FindHighestTemprature(IList<TempratureInfo> tempratures)
{
if(tempratures.Count == 0)
throw new InvalidOperationException("tempratures");
return tempratures.OrderByDescending(t => t.Temprature)
.First();
}
private static TempratureInfo FindLowestTemprature(IList<TempratureInfo> tempratures)
{
if (tempratures.Count == 0)
throw new InvalidOperationException("tempratures");
return tempratures.OrderBy(t => t.Temprature)
.First();
}
您可以将Info
类扩展为覆盖ToString
,当您期望字符串,参数传递,变量赋值等调用上述函数中的任何一个时, ToString
将返回格式化的字符串。
internal class TempratureInfo
{
public string Name { get; set; }
public double Temprature { get; set; }
public override string ToString()
{
return string.Format("Temprature in {0} is currently {1}", Name, Temprature);
}
}
示例实施:
private static void Main(string[] args)
{
List<TempratureInfo> unitedKingdomTempratures = new List<TempratureInfo>()
{
{new TempratureInfo() { Name = "Wales", Temprature = 10.0}},
{new TempratureInfo() { Name = "England", Temprature = 55.0}},
{new TempratureInfo() { Name = "Scotland", Temprature = 22.0}},
{new TempratureInfo() { Name = "Northen Ireland", Temprature = 4.0}}
};
Console.WriteLine("Hottest: " + FindHighestTemprature(unitedKingdomTempratures).Name);
Console.WriteLine("Coldest: " + FindLowestTemprature(unitedKingdomTempratures).Name);
Console.ReadLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.