C# 比较多个arraylist长度以找到最长的一个

C# 比较多个arraylist长度以找到最长的一个,c#,windows,vb.net,arrays,list,C#,Windows,Vb.net,Arrays,List,我有6个数组列表,我想知道哪一个是最长的,而不使用一堆IF语句 “if arraylist.count>anotherlist.count Then…”“if语句是否可以接受1 public ArrayList FindLongest(params ArrayList[] lists) { var longest = lists[0]; for(var i=1;i<lists.Length;i++) { if(lists[i].Length > lon

我有6个数组列表,我想知道哪一个是最长的,而不使用一堆IF语句


“if arraylist.count>anotherlist.count Then…”“if语句是否可以接受1

public ArrayList FindLongest(params ArrayList[] lists)
{
   var longest = lists[0];
   for(var i=1;i<lists.Length;i++)
   {
       if(lists[i].Length > longest.Length)
          longest = lists[i];
   }
   return longest;
}
public ArrayList FindLongest(参数ArrayList[]列表)
{
var longest=列表[0];
对于(变量i=1;i最长.Length)
最长=列表[i];
}
返回时间最长;
}

如果
语句可接受,1是否为

public ArrayList FindLongest(params ArrayList[] lists)
{
   var longest = lists[0];
   for(var i=1;i<lists.Length;i++)
   {
       if(lists[i].Length > longest.Length)
          longest = lists[i];
   }
   return longest;
}
public ArrayList FindLongest(参数ArrayList[]列表)
{
var longest=列表[0];
对于(变量i=1;i最长.Length)
最长=列表[i];
}
返回时间最长;
}
您可以使用Linq:

public static ArrayList FindLongest(params ArrayList[] lists)
{
    return lists == null 
        ? null
        : lists.OrderByDescending(x => x.Count).FirstOrDefault();
}

如果您只想知道最长列表的长度,则更简单:

public static int FindLongestLength(params ArrayList[] lists)
{
    return lists == null 
        ? -1 // here you could also return (int?)null,
             // all you need to do is adjusting the return type
        : lists.Max(x => x.Count);
}
您可以使用Linq:

public static ArrayList FindLongest(params ArrayList[] lists)
{
    return lists == null 
        ? null
        : lists.OrderByDescending(x => x.Count).FirstOrDefault();
}

如果您只想知道最长列表的长度,则更简单:

public static int FindLongestLength(params ArrayList[] lists)
{
    return lists == null 
        ? -1 // here you could also return (int?)null,
             // all you need to do is adjusting the return type
        : lists.Max(x => x.Count);
}

如果您将所有内容存储在列表中,例如

List<List<int>> f = new List<List<int>>();
List f=新列表();
然后是一只林克象

List<int> myLongest = f.OrderBy(x => x.Count).Last();
List myLongest=f.OrderBy(x=>x.Count).Last();

将生成项目数最多的列表。当然,如果您将所有内容都存储在列表中,例如

List<List<int>> f = new List<List<int>>();
List f=新列表();
然后是一只林克象

List<int> myLongest = f.OrderBy(x => x.Count).Last();
List myLongest=f.OrderBy(x=>x.Count).Last();

将生成项目数最多的列表。当然,如果您只想知道最长ArrayList的长度,则必须处理最长列表出现平局的情况:

public int FindLongest(params ArrayList[] lists)
{
    return lists.Max(item => item.Count);
}
或者,如果您不想编写函数,只想将代码串联起来,那么:

int longestLength = (new ArrayList[] { arraylist1, arraylist2, arraylist3, 
    arraylist4, arraylist5, arraylist6 }).Max(item => item.Count);

如果只需要最长ArrayList的长度:

public int FindLongest(params ArrayList[] lists)
{
    return lists.Max(item => item.Count);
}
或者,如果您不想编写函数,只想将代码串联起来,那么:

int longestLength = (new ArrayList[] { arraylist1, arraylist2, arraylist3, 
    arraylist4, arraylist5, arraylist6 }).Max(item => item.Count);

您使用的是什么版本的.NET,确切的类型是什么?(示例代码很好…)4.0。c#.net或vb.net示例非常好。您使用的是.net的哪个版本,确切的类型是什么?(示例代码很好…)4.0。c#.net或vb.net示例很好。您是指
foreach
?另外,
Count
必须以大写字母
C
开头。你是说
foreach
?此外,
Count
必须以大写字母
C
开头。intellisence中不显示项。是否有我需要的参考资料?您需要使用System.Linq添加
项未出现在intellisence中。是否有我需要的参考资料?您需要使用System.Linq添加