牛骨文教育服务平台(让学习变的简单)
博文笔记

c# 一维数组和二维数组的定义几种方式

创建时间:2016-04-05 投稿人: 浏览次数:2397

http://blog.csdn.net/hwm831002/article/details/8782445

s为一个二维数组。
s.GetLength(0) 返回第一维的长度(即行数)
s.GetLength(1) 返回第二维的长度(即列数)

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            //一维数组定义与初始化  
            int[] one1 = new int[] {3,2,1 };//第一种方式  
            int[] one2 = { 3, 2, 1 };      //第二种方式  
            int[] one3;                    //第三种方式  
            one3=new int[]{3,2,1};  
  
            
            //二维数组定义与初始化  
  
            //不规则二维数组  
            int[][] array = new int[2][];  
            array[0] = new int[3];  
            array[0][1] = 11;  
            array[0][2] = 12;  
            array[1] = new int[] { 1, 2, 3, 4,5 };  
  
            //或int[][] array = new int[2][] { new int[] {0,11,12 }, new int[] {1,2,3,4,5 }};  
            
            Console.WriteLine( "不规则二维数组:  ");  
            for (int i = 0; i < array.Length; i++)  
            {  
                foreach (int j in array[i])  
                {  
                    Console.Write(j+"   ");  
                }  
                Console.WriteLine();  
            }  
  
            //固定长度的矩阵数组  
            int[,] arrray1=new int[2,5]{{9,9,9,9,0},{0,0,9,0,0}};  
            Console.WriteLine("规则二维数组输出方法一:  ");  
            for (int ii = 0; ii < 2; ii++)                        //输出方法一  
            {  
                for (int j = 0; j < 5; j++)  
                {  
                    Console.Write(arrray1[ii,j] + "   ");  
                }  
                Console.WriteLine();  
            }  
            Console.WriteLine("规则二维数组输出方法二:  ");  
            foreach (int j in arrray1)//arrray.length=10;      //输出方法二  
            {  
                Console.Write(j + "   ");  
            }  
            Console.WriteLine();  
            Console.ReadLine();  
        }  
    }  
}  


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。