[英]find the highest number in an array made by the method
There is a class for passengers which contain string name
, int age
, string job
etc. I made an array of this class, lets say it has 10 places.有一个 class 供乘客使用,其中包含
string name
、 int age
、 string job
等。我制作了这个 class 的数组,假设它有 10 个位置。 I wanna find the oldest passenger.我想找到最年长的乘客。
My code doesn't work, because it is not possible to compare passenger[i]
with an integer.我的代码不起作用,因为无法将
passenger[i]
与 integer 进行比较。 I mean I need only the age
in passenger[]
我的意思是我只需要
passenger[]
的age
[]
How to find the oldest one in passenger[]
?如何在
passenger[]
中找到最旧的?
EDIT: The return value should be a passenger
by his name
and major
, not only its age
.编辑:返回值应该是他的
name
和major
的passenger
,而不仅仅是它的age
。
public Passenger Oldest()
{
int oldest = 0;
for (int i = 0; i < passengers.Length; i++)
{
if (passengers[i] > oldest)
{
oldest = passengers[i];
}
}
return oldest;
}
class Passenger
{
int age;
string name;
string major;
public Passenger(int _age, string _name, string _major)
{
age = _age;
name = _name;
major = _major;
}
}
Firstly, as mentioned by @Cid in comment to question all fields of Passenger
are private
(by default when modifier is not specified).首先,正如@Cid 在评论中提到的,质疑
Passenger
的所有字段都是private
的(默认情况下,当未指定修饰符时)。 You should either mark then as public
(to access them outside declaring class) or better create public properties:您应该将 then 标记为
public
(以在声明类之外访问它们)或更好地创建公共属性:
class Passenger
{
public int Age { get; set; } // public auto property
public string Name { get; set; } // public auto property
public string Major { get; set; } // public auto property
public Passenger(int age, string name, string major)
{
Age = age;
Name = name;
Major = major;
}
}
Secondly, you need to compare Age
(proeprty, not whole object) of the passenger:其次,您需要比较乘客的
Age
(财产,而不是整个对象):
if (passengers[i].Age > oldest)
{
oldest = passengers[i].Age;
}
Also, you could use LINQ
to find the oldest passenger:此外,您可以使用
LINQ
找到最年长的乘客:
var oldest = passengers.Max(item => item.Age);
Finally, to return the oldest passenger:最后,返回最年长的乘客:
public Passenger Oldest()
{
// if no passengers -> return null
if (!passengers?.Any() ?? true)
{
return null;
}
var maxAge = passengers.Max(item => item.Age);
return passengers.First(item => item.Age == maxAge);
}
Also, as mentioned by @DmitryBychenko the method can be shortened to:此外,正如@DmitryBychenko 所述,该方法可以缩短为:
public Passenger Oldest()
{
// if no passengers -> return null
if (!passengers?.Any() ?? true)
{
return null;
}
return passengers.Aggregate((s, a) => s.Age > a.Age ? s : a);
}
or without LINQ
:或没有
LINQ
:
public Passenger Oldest()
{
// if no passengers -> return null
if (passengers == null || passengers.Length == 0)
{
return null;
}
var maxAge = passengers[0].Age;
var oldestPassengerIndex = 0;
for (var i = 1; i < passengers.Length; i++)
{
if (passengers[i].Age > maxAge)
{
oldest = passengers[i].Age;
oldestPassengerIndex = i;
}
}
return passengers[oldestPassengerIndex];
}
Using Linq is easy使用 Linq 很容易
var oldest=passengers.Max(x=>x.Age):
Otherwise否则
Passenger oldest=new Passenger(0,"","");
foreach (Passenger p in Passengers){
if (p.age>oldest.age) oldest=p;
}
Slightly more efficient version of Roman's answer, especially if the oldest passenger appears last in the list:罗曼答案的稍微更有效的版本,特别是如果最年长的乘客出现在列表的最后:
public Passenger Oldest()
{
if (passengers.Length == 1) return passengers[0];
var oldest = passengers[0];
for (int i = 1; i < passengers.Length; i++)
{
if (passengers[i].Age > oldest.Age) oldest = passengers[i];
}
return oldest;
}
This only ever requires a single iteration.这只需要一次迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.