C#数组

 

一、数组
1. 数组概述
• C# 数组从零开始建立索引,即数组索引
从零开始。
• 声明数组时,方括号([]) 必须跟在类型后
面,而不是标识符后面。
• 数组的大小不是其类型的一部分
• 例如:
• int[] numbers;
• numbers = new int[10];
• numbers = new int[20];
• 数组使用需要初始化

2.数组是对象
• 在C# 中,数组实际上是对象。System.Array
是所有数组类型的抽象基类型。
• 可以使用System.Array 具有的属性以及其他类
成员。这种用法的一个示例是使用“长度”(Length)
属性获取数组的长度。
下面的代码将numbers 数组的长度(为5)赋给
名为LengthOfNumbers 的变量:
int[] numbers = {1, 2, 3, 4, 5}; int
LengthOfNumbers = numbers.Length;
• System.Array 类提供许多有用的其他方法/属
性,如用于排序、搜索和复制数组的方法。

3.多维数组
• 使用多个下标访问其元素的数组
• 二维数组:
– 平面上点的坐标pt(x,y)
– 声明:
• double[,] dbHeight=new double[3,4];
• double[,] dbHeight={ {1,2,3,4},{2,3,4,5},{3,4,5,6} };

4.数组的数组
• 数组的每个元素是数组
• 声明方式
int[][] arrayInt;
– 方式一:
arrayInt=new int[2][];
//注意不能用arrayInt=new int[3][4]!
arrayInt[0]=new int[3];
arrayInt[1]=new int[4];
– 方式二:
arrayInt={new int[]{1,2,3,},new int[]{4,5,6,7}};

参数数组
• 参数的类型
– 值参数
– 引用参数
– 输出参数
• 一种新类型的参数:参数数组
– 可以使用个数不定的参数调用函数
– 必须是参数定义中的最后一个参数
– 用关键字params说明
– params参数必须是一维数组
– 例如:
public void fun(params in[]args)

下面用一个Demo来对上述所提到的数组进行演示:

 

代码
using System;
using System.Collections.Generic;
using System.Text;

namespace ArrayDemo
{
    
class Program
    {
        
static void ArrayTest()
        {
            
string[] friendNames = {"小张","小李","小赵"};
            Console.WriteLine(
"我的有{0}位朋友;", friendNames.Length);
            
for(int i=0;i<friendNames.Length;i++)
            {
                Console.WriteLine(friendNames[i]);
            }

        }
        
static void ArrayTestM()
        {
            
int[,] nArrayHeight = { { 1234 }, { 5678 }, { 9101112 } };
            
foreach (int n in nArrayHeight)
            {
                Console.WriteLine(
"{0}", n);
            }
            Console.WriteLine(
"二维数组输出结束!");
        }
        
static void ArrayOfArray()
        {
            
int[][] arrayA =new int[] { 1 }, new int[] { 12 }, new int[] { 123 }, new int[] { 1234 }, new int[] { 12345 } };
            
foreach (int[] ArrayN in arrayA)
            {
                
foreach (int n in ArrayN)
                {
                    Console.WriteLine(
"{0}", n);
                }                
            }
            Console.WriteLine(
"数组的数组输出结束!");
        }
        
static int sumValsN(int[] nVals)
        {
            
int nSum = 0;
            
foreach (int val in nVals)
            {
                nSum 
+= val;
            }
            
return nSum;
        }
        
static int sumVals(params int[] nVals)
        {
            
int nSum = 0;
            
foreach (int val in nVals)
            {
                nSum 
+= val;
            }
            
return nSum;
        }
        
static void Main(string[] args)
        {
            ArrayTest();
            ArrayTestM();
            ArrayOfArray();
            
int[] arrayN = new int[]{1,2,3,4,5};
            
int nResult = sumValsN(arrayN);
            Console.WriteLine(
"和为:{0}", nResult);       
            nResult 
= sumVals(12347);
            Console.WriteLine(
"和为:{0}", nResult);
            Console.ReadLine();
        }
    }
}

 

 

posted @ 2010-08-06 11:40  魄力  阅读(2595)  评论(0编辑  收藏  举报