C#遍历数组

Eg:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //声明数组. 第一种方法. 声明并分配元素大小.
13             int[] Myint = new int[30];
14             Myint[0] = 30;
15             Myint[1] = 50;
16             // 以此类推, 起始下标为0
17             //-------------------------------
18             // 声明数组,第二种方法, 声明并直接赋值,没有指定元素大小.
19             int[] Myint1 = { 20,10,50,65,18,90}; 
20             //------------------------------------------
21             //声明数组,第三种方法, 声明并分配大小,且赋值.
22             int[] i = new int[5] { 10, 20, 30, 40, 50 };
23 
24             // -----------------------------------------------
25             // foreach循环遍历数组..
26             int[] Sum = new int[50];
27             Random rd = new Random();
28            // 先用for循环给数组取随机数.
29             for (int s = 0; s <= Sum.Length - 1; s++)  // Sum.Length是数组的一个属性,Length代表数组的长度
30             {
31                 Sum[s] = rd.Next(100);
32             }
33            // 遍历数组输出
34             foreach (int t in Sum)
35             {
36                 Console.WriteLine(t);
37             }
38         }
39     }
40 }

 

posted @ 2016-06-28 22:04  AnQi_安琪_小屋  阅读(4420)  评论(1编辑  收藏  举报