c# 得到list符合某条件的索引值,排序

请教,在List集合中怎么得到元素的索引值,参考:http://www.myexception.cn/c-sharp/385022.html

这个可以用来读取窗口的多个textbox控件中内容:

 1 ------解决方案--------------------
 2 你可以使用FindIndex方法
 3 C# code
 4  private void Form1_Load(object sender, EventArgs e)
 5     {
 6 
 7       TextBox textBox1 = new TextBox();
 8       textBox1.Name = "X1";
 9       TextBox textBox2 = new TextBox();
10       textBox2.Name = "X2";
11       TextBox textBox3 = new TextBox();
12       textBox3.Name = "X3";
13       TextBox textBox4 = new TextBox();
14       textBox4.Name = "X4";
15       List<TextBox> list = new List<TextBox> { textBox1, textBox2, textBox3, textBox4 };
16       int x = list.FindIndex(GetTextBox);
17       MessageBox.Show(x.ToString());
18 
19     }
20 
21     private static bool GetTextBox(TextBox s)
22     {
23       if (s.Name == "X2")
24       {
25         return true;
26       }
27       else
28       {
29         return false;
30       }
31     }
读取textbox内容进list

c#中List <int[]>集合添加和查找元素,参考:http://blog.csdn.net/jinjazz/article/details/2387552


c#常用的数据存储分析参考:http://blog.csdn.net/zzmkljd/article/details/52343947


下面对list元素与形参相减排序得到与形参最接近的list元素:
 1     static double  TestIndex(double num)//定义函数查找list的与某元素最接近的值或值的索引
 2     {
 3         List<double> list = new List<double>();//新定义一个list
 4         list.Add(0.0158);//向list中添加数据
 5         list.Add(0.0238);
 6         list.Add(0.0315);
 7         list.Add(0.0446);
 8         //下面通过select语句与匿名函数找到与形参最接近的值
 9         return list.Select((d, i) =>
10         {
11             return new
12             {
13                 Value = d,
14                 Index = i
15             };
16         }).OrderBy(x => Math.Abs(x.Value - num)).First().Value ;//.First().Index;可以得到索引位置,返回值要改为int
17     }
View Code

http://bbs.csdn.net/topics/391078319

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1shiyan
 8 {
 9 class  Program
10       {
11     static double  TestIndex(double num)//定义函数查找list的与某元素最接近的值或值的索引
12     {
13         List<double> list = new List<double>();//新定义一个list
14         list.Add(0.0158);//向list中添加数据
15         list.Add(0.0238);
16         list.Add(0.0315);
17         list.Add(0.0446);
18         //下面通过select语句与匿名函数找到与形参最接近的值
19         return list.Select((d, i) =>
20         {
21             return new
22             {
23                 Value = d,
24                 Index = i
25             };
26         }).OrderBy(x => Math.Abs(x.Value - num)).First().Value ;//.First().Index;可以得到索引位置,返回值要改为int
27     }
28 
29           static   void  Main( string [] args)
30           {
31 
32               double wo=TestIndex(0.0316);
33              List < int [] >  alSchedule  =   new  List < int [] > (); // 声明一个存放int[]元素的 集合 
34              alSchedule.Add( new   int []  {  1 ,  2 ,  3  } );
35              alSchedule.Add( new   int []  {  1 ,  2 ,  3 ,  4  } );
36               int [] result  =  alSchedule.Find(FindElements);
37               if  (result  !=   null )
38               {
39                  Console.WriteLine(result);
40              } 
41              Console.Read();
42          } 
43           private   static   bool  FindElements( int [] arrInt)
44           {
45               return  IsEquals(arrInt,  new   int []  {  1 ,  2 ,  3  } );
46          } 
47           public   static   bool  IsEquals(Array array1, Array array2)
48           {
49               // 比较类型是否一样  
50               if  ( ! Object.ReferenceEquals(array1.GetType(), array2.GetType()))
51               {
52                   return   false ;
53              } 
54 
55               // 比较长度是否一样  
56               if  (array1.GetLength( 0 )  !=  array2.GetLength( 0 ))
57               {
58                   return   false ;
59              } 
60 
61               // 比较成员是否对应相等  
62              ValueType v1, v2;
63               for  ( int  i  =   0 ; i  <  array1.GetLength( 0 ); i ++ )
64               {
65                  v1  =  (ValueType)array1.GetValue(i);
66                  v2  =  (ValueType)array2.GetValue(i);
67 
68                   if  ( ! v1.Equals(v2))
69                   {
70                       return   false ;
71                  } 
72              } 
73               return   true ;
74          }  
75 
76      } 
77 }
判断某list是不是list的元素

http://blog.csdn.net/jinjazz/article/details/2387552


各种变量声明时默认值:

 1 class MyVar { 
 2     /*
 3      * 基于安全的考虑,c#变量的初始化有一定的要求
 4      * 1.所有的局部变量在被显示的初始化之前,都会被编译器当作未初始化,然后抛出编译期出错;
 5      * 2.所有的字段级变量被编译器初始化为所属类型中等价于0的值.如布尔型的初始化为false,数值型的初始化为
 6 或者0.0,所有引用类型都初始化为null.
 7      * */
 8         private string Name;
 9         public void SaySomthing() {
10             string info;
11             Console.WriteLine(info);
12         }
13         /*
14          * 上面的代码中,Name是字段级变量,info是局部变量
15          * 在编译的时候就会报错.(使用了未赋值的info)
16          * 因为局部变量在使用时是必须要赋值的,而字段级的变量可以被编译器自动初始化.         * 
17          */
18         /*
19          * 各种数据类型的默认值
20          * 整型的都返回0
21          * ushort 0
22          * ulong 0
23          * uint 0
24          * struct 整型的0,引用型的null
25          * short 0
26          * sbyte 0
27          * long 0
28          * int 0
29          * float 0.0F
30          * enum 枚举类型下标为0的元素
31          * double 0.0D
32          * decimal 0.0M
33          * char ''
34          * byte 0
35          * bool flase
36          */
37         /*
38          从C#3.0开始,引入了var关键字,编译器可以通过它的初始值来判断具体类型.
39          * 根据上面的综述,var只能用于局部变量的声明,不能用于字段级别的声明,并且var声明的变量必须要有初始值,这样编译器才能判断变量的类型.
40          */
41     }
默认值

http://www.cnblogs.com/guoyansi19900907/p/3664058.html


 快速排序法,效率高:http://www.jb51.net/article/86442.htm

原文地址:https://www.cnblogs.com/zhubinglong/p/8287749.html