13-C#笔记-数组

# 1 初始化

double[] balance = new double[10]; // 隐式初始化为0
double[] balance = { 2340.0, 4523.69, 3421.0}; // 这个就很简洁
int [] marks = new int[5]  { 99,  98, 92, 97, 95}; // 多余,不用delete
int [] marks = new int[]  { 99,  98, 92, 97, 95}; // 多余

int [] marks = new int[]  { 99,  98, 92, 97, 95};
int[] score = marks;

  

 

# 2. 访问

使用中括号,同C++;

 

foreach 示例:

         int []  n = new int[10]; /* n 是一个带有 10 个整数的数组 */

         /* 输出每个数组元素的值 */
         foreach (int j in n )
         {
            int i = j-100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
         }
         Console.ReadKey();

  

# 3 二维数组

int [,] a = new int [3,4] {
 {0, 1, 2, 3} ,   /*  初始化索引号为 0 的行 */
 {4, 5, 6, 7} ,   /*  初始化索引号为 1 的行 */
 {8, 9, 10, 11}   /*  初始化索引号为 2 的行 */
};
int val = a[2,3]; // 访问

  

 # 4 交错数组

int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};

  

# 5 数组作为函数输入

using System;

namespace ArrayApplication
{
   class MyArray
   {
      double getAverage(int[] arr, int size)
      {
         int i;
         double avg;
         int sum = 0;

         for (i = 0; i < size; ++i)
         {
            sum += arr[i];
         }

         avg = (double)sum / size;
         return avg;
      }
      static void Main(string[] args)
      {
         MyArray app = new MyArray();
         /* 一个带有 5 个元素的 int 数组 */
         int [] balance = new int[]{1000, 2, 3, 17, 50};
         double avg;

         /* 传递数组的指针作为参数 */
         avg = app.getAverage(balance, 5 ) ;

         /* 输出返回值 */
         Console.WriteLine( "平均值是: {0} ", avg );
         Console.ReadKey();
      }
   }
}

  

# 6 数组作为形参

using System;

namespace ArrayApplication
{
   class ParamArray
   {
      public int AddElements(params int[] arr)
      {
         int sum = 0;
         foreach (int i in arr)
         {
            sum += i;
         }
         return sum;
      }
   }
      
   class TestClass
   {
      static void Main(string[] args)
      {
         ParamArray app = new ParamArray();
         int sum = app.AddElements(512, 720, 250, 567, 889);
         Console.WriteLine("总和是: {0}", sum);
         Console.ReadKey();
      }
   }
}

  

# 7 Array类的成员函数

1 Clear
根据元素的类型,设置数组中某个范围的元素为零、为 false 或者为 null。
2 Copy(Array, Array, Int32)
从数组的第一个元素开始复制某个范围的元素到另一个数组的第一个元素位置。长度由一个 32 位整数指定。
3 CopyTo(Array, Int32)
从当前的一维数组中复制所有的元素到一个指定的一维数组的指定索引位置。索引由一个 32 位整数指定。
4 GetLength 
获取一个 32 位整数,该值表示指定维度的数组中的元素总数。
5 GetLongLength
获取一个 64 位整数,该值表示指定维度的数组中的元素总数。
6 GetLowerBound
获取数组中指定维度的下界。
7 GetType
获取当前实例的类型。从对象(Object)继承。
8 GetUpperBound
获取数组中指定维度的上界。
9 GetValue(Int32)
获取一维数组中指定位置的值。索引由一个 32 位整数指定。
10 IndexOf(Array, Object)
搜索指定的对象,返回整个一维数组中第一次出现的索引。
11 Reverse(Array)
逆转整个一维数组中元素的顺序。
12 SetValue(Object, Int32)
给一维数组中指定位置的元素设置值。索引由一个 32 位整数指定。
13 Sort(Array)
使用数组的每个元素的 IComparable 实现来排序整个一维数组中的元素。
14 ToString
返回一个表示当前对象的字符串。从对象(Object)继承。

示例:

            int[] list = { 34, 72, 13, 44, 25, 30, 10 };

            // 逆转数组
            Array.Reverse(list);
            
            // 排序数组
            Array.Sort(list);
           

  

 

参考:

http://www.runoob.com/csharp/csharp-array.html

 

posted @ 2018-05-21 16:31  路边的十元钱硬币  阅读(126)  评论(0编辑  收藏  举报