c#中List <int[]>集合添加和查找元素
2010-02-09 14:18
369 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
... {
class Program
... {
static void Main( string [] args)
... {
List < int [] > alSchedule = new List < int [] > (); // 声明一个存放int[]元素的 集合
alSchedule.Add( new int [] ... { 1 , 2 , 3 } );
alSchedule.Add( new int [] ... { 1 , 2 , 3 , 4 } );
int [] result = alSchedule.Find(FindElements);
if (result != null )
... {
Console.WriteLine(result);
}
Console.Read();
}
private static bool FindElements( int [] arrInt)
... {
return IsEquals(arrInt, new int [] ... { 1 , 2 , 3 } );
}
public static bool IsEquals(Array array1, Array array2)
... {
// 比较类型是否一样
if ( ! Object.ReferenceEquals(array1.GetType(), array2.GetType()))
... {
return false ;
}
// 比较长度是否一样
if (array1.GetLength( 0 ) != array2.GetLength( 0 ))
... {
return false ;
}
// 比较成员是否对应相等
ValueType v1, v2;
for ( int i = 0 ; i < array1.GetLength( 0 ); i ++ )
... {
v1 = (ValueType)array1.GetValue(i);
v2 = (ValueType)array2.GetValue(i);
if ( ! v1.Equals(v2))
... {
return false ;
}
}
return true ;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
... {
class Program
... {
static void Main( string [] args)
... {
List < int [] > alSchedule = new List < int [] > (); // 声明一个存放int[]元素的 集合
alSchedule.Add( new int [] ... { 1 , 2 , 3 } );
alSchedule.Add( new int [] ... { 1 , 2 , 3 , 4 } );
int [] result = alSchedule.Find(FindElements);
if (result != null )
... {
Console.WriteLine(result);
}
Console.Read();
}
private static bool FindElements( int [] arrInt)
... {
return IsEquals(arrInt, new int [] ... { 1 , 2 , 3 } );
}
public static bool IsEquals(Array array1, Array array2)
... {
// 比较类型是否一样
if ( ! Object.ReferenceEquals(array1.GetType(), array2.GetType()))
... {
return false ;
}
// 比较长度是否一样
if (array1.GetLength( 0 ) != array2.GetLength( 0 ))
... {
return false ;
}
// 比较成员是否对应相等
ValueType v1, v2;
for ( int i = 0 ; i < array1.GetLength( 0 ); i ++ )
... {
v1 = (ValueType)array1.GetValue(i);
v2 = (ValueType)array2.GetValue(i);
if ( ! v1.Equals(v2))
... {
return false ;
}
}
return true ;
}
}
}
相关文章推荐
- c#中List <int[]>集合添加和查找元素
- c#中List <int[]>集合添加和查找元素
- 集合框架(一)续 向集合中添加一个集合、数组和List之间的相互转换、从集合中删除另一个集合所包含的元素
- C#控制台基础 int类型list集合forEach方法实例
- list集合添加元素,删除元素
- 查找List,Array中元素的其他C#写法
- 将元素添加到List集合的第一位
- 将list集合的元素按照添加顺序的倒序进行排列取出
- C#中list泛型中防止添加重复元素的解决办法(2014-06-11)
- C#控制台基础 list<int>添加元素并输出实例
- C# 找出泛型集合中的满足一定条件的元素 List<T>.Wher()
- 数据数去重和将list集合中的元素添加到数组中
- C# 找出泛型集合中的满足一定条件的元素 List.Wher()
- c# Array或List有个很实用的ForEach方法,可以直接传入一个方法对集合中元素操作
- C#.NET底层 List 集合循环添加对象(如:list.add(model)),为什么会出现整个list集合都是一样的数据?
- List集合,泛型,将元素全部添加到集合中
- C#控制台基础 检测list泛型集合中是否不存在元素
- C#中判断list集合中元素的包含关系
- 在list集合中的添加、修改、删除和遍历元素
- C#将Json字符串反序列化成List对象类集合