简体   繁体   中英

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. I wanna find the oldest passenger.

My code doesn't work, because it is not possible to compare passenger[i] with an integer. I mean I need only the age in passenger[]

How to find the oldest one in passenger[] ?

EDIT: The return value should be a passenger by his name and major , not only its 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). You should either mark then as public (to access them outside declaring class) or better create public properties:

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:

if (passengers[i].Age > oldest)
{
    oldest = passengers[i].Age;
}

Also, you could use LINQ to find the oldest passenger:

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:

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 :

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM