C# Array GetValue,SetValue,GetLowerBound,GetUpperBound的学习

 1         static void Main(string[] args)
 2         {
 3             //Array.Find
 4             //搜索与指定条件match相匹配的项,然后输出第一个匹配项
 5             //Array.Find(Array1,match);
 6 
 7             int i, j, k;
 8             int[] a = new int[] { 1,2,3,4,5};
 9             char[] b=new char [] {'1','2','3','4','5' };
10             int[,] aa = new int[,] { {1,2,3,4 },{5,6,7,8 } };
11             // Array.Find(a, V);不会使用
12 
13 
14             //GetLowerBound
15             //返回指定维的下界
16             //GetUpperBound
17             //返回指定维的上界
18             //使用方法都一致,是Array1.GetLowerBound(dimension)
19             //dimension为数组的维度
20 
21             //我们可以用齐来简化一些操作
22             //例如这的a是一个一维数组,所以他对应的维度只有一个0
23             for (i= a.GetLowerBound(0); i<=a.GetUpperBound(0);i++)
24                
25             {
26                 Console.WriteLine("{0}",a[i]);
27             }
28 
29             //如果我们对dimension做一些改变
30             Console.WriteLine("//////////////////////////////////////////////////////////////////");
31 
32             //这里就是二维的数组的第二维数
33             for (i = aa.GetLowerBound(1); i <= aa.GetUpperBound(1); i++)
34 
35             {
36                 Console.WriteLine("{0}", aa[1,i]);
37             }
38 
39             Console.WriteLine("//////////////////////////////////////////////////////////////////");
40             //SetValue和GetValue
41             a = new int[] { 1, 2, 3, 4, 5 };
42              b = new char[] { '1', '2', '3', '4', '5' };
43              aa = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
44 
45             Console.WriteLine("{0}", a.GetValue(2));//3
46             //GetValue,获得对应于()内的位数的数据,这个只能用于一维数组
47             a.SetValue(2, 0);
48             Console.WriteLine("{0}", a.GetValue(0));//2
49                                                     //SetValue,(数据,改变数据的位置)的形式
50 
51             /*
52             b.SetValue(2,1);
53             Console.WriteLine("{0}", b.GetValue(1));
54             这里不能这么写,虽然SetValue没有报错,但是b数组是一个char型的数组,
55             不可以再里面写int型。
56              
57              */
58             b.SetValue('2', 0);//以字符型就可以输出。
59             Console.WriteLine("{0}", b.GetValue(0));
60 
61             Console.WriteLine("//////////////////////////////////////////////////////////////////");
62 
63             //那对字符串类型的呢?
64             string[] aaa = new string[] {"123A","456B","789D","WS","SB" };
65             string[] bbb = new string[] { "123A456B789D" };
66             //有aaa和bbb两种类型的字符串数组
67             Console.WriteLine("{0}", aaa.GetValue(2));
68             //输出的是789D
69             //Console.WriteLine("{0}", bbb.GetValue(2));
70             //这里语法上面没有错误,但是输出出来就有问题了GetValue会认为一个""就是一个字符串数组,
71             //所以我们这里的GetValue(2)是越界了
72             Console.WriteLine("{0}", bbb.GetValue(0));
73             //输出结果就是123A456B789D
74 
75             //同理SetValue就同样的方法
76             aaa.SetValue("SB",0);
77             Console.WriteLine("{0}", aaa.GetValue(0));
78 
79             //aaa.SetValue('S', 1);
80             //Console.WriteLine("{0}", aaa.GetValue(1));
81             //aaa.SetValue(1,2);
82             //Console.WriteLine("{0}", aaa.GetValue(2));
83             //这两个都是有问题的,注意,这里不会相互转换,只能是什么就填什么,什么类型的数据对应什么类型是数据
84 
85             bbb.SetValue("SB", 0);
86             Console.WriteLine("{0}",bbb .GetValue(0));
87             //所以bbb字符串只能整体变动,而不能向一个单个是字符串一样可以单个字符的变动。
88 
89 
90 
91 
92 
93 
94 
95         }

 

posted @ 2020-09-11 15:03  想活出点人样  阅读(570)  评论(0编辑  收藏  举报