c# 一維數組和二維數組的定義幾種方式<轉>.

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();
        }
    }
}
相關文章
相關標籤/搜索