C#學習筆記之數組操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Array intArray1 = Array.CreateInstance(typeof(int), 5);
            for (int i = 0; i < 5; i++)
                intArray1.SetValue(33, i);
            for (int i = 0; i < 5; i++)
                Console.WriteLine(intArray1.GetValue(i));
            Console.ReadLine();

            int[] lengths={2,3};
            int[] lowerBounds={1,10};
            //創建一個2*3的數組,數組的每一維都是從lowerBounds所指的數字開始的。
            Array intArray2 = Array.CreateInstance(typeof(int), lengths, lowerBounds);
            intArray2.SetValue(55, 1, 10);
            intArray2.SetValue(33,1,11);
            intArray2.SetValue(22, 1, 12);

            intArray2.SetValue(11, 2, 10);
            intArray2.SetValue(66, 2, 11);
            intArray2.SetValue(77, 2, 12);

            for (int i = 1; i < 3; i++)
            {
                for (int j = 10; j < 13; j++)
                    Console.Write(" {0}", intArray2.GetValue(i, j));
                Console.WriteLine();
            }               
            Console.ReadLine();

            int[] array = new int[] {1,2,3 };
            int[] array1 = new int[2] {1,2 };

            //鋸齒
            int[][] intArray3 = new int[3][];//行數一定要指定
            intArray3[0] = new int[2] {1,2 };
            intArray3[1] = new int[5] {3,4,5,6,7 };
            intArray3[2] = new int[3] {8,9,0 };
            
            for(int row=0;row<intArray3.Length;row++)
            {
                Console.Write("row {0}:",row);
                for (int col = 0; col < intArray3[row].Length; col++)
                    Console.Write(" {0}", intArray3[row][col]);
                Console.WriteLine();
                Console.ReadLine();
            }

            //二維數組
            int[,] intArray4 = {
                               {1,2,3},
                               {4,5,6},
                               {7,8,9}
                               };
            int[,] intArray5 = new int[3, 3];
            //三維數組
            int[, ,] intArray6 ={
                              {{1,1},{2,2}},
                              {{3,3},{4,4}},
                              {{5,5},{6,6}},
                              };

        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章