C#数组
来源:互联网 发布:php自定义加密 编辑:程序博客网 时间:2024/05/16 10:00
- using System;
- namespace testArrayApp
- {
- /// <summary>
-
-
- class Class1
- {
- /// <summary>
-
-
- [STAThread]
- static void Main(string[] args)
- {
-
-
-
-
- int[] intArray1;
-
- intArray1 = new int[3];
- intArray1 = new int[3]{1,2,3};
- intArray1 = new int[]{1,2,3};
-
-
- int[] intArray2 = new int[3]{1,2,3};
- int[] intArray3 = new int[]{4,3,2,1};
- int[] intArray4 = {1,2,3,4};
- string[] strArray1 = new string[]{"One","Two","Three"};
- string[] strArray2 = {"This","is","an","string","Array"};
-
-
- if (intArray1[2] > intArray2[0])
- {
-
- Write_1DArray(intArray1);
- Write_1DArray(strArray2);
- }
-
- Write_1DArray(new int[]{2,3,4,5});
- Write_1DArray(new String[]{"Hello","My","Friends"});
-
-
- short[,] sArray1;
-
- sArray1 = new short[2,2];
- sArray1 = new short[2,2]{{1,1},{2,2}};
- sArray1 = new short[,]{{1,2,3},{4,5,6}};
-
-
- short[,] sArray2 = new short [1,1]{{100}};
- short[,] sArray3 = new short [,]{{1,2},{3,4},{5,6}};
- short[,] sArray4 = {{1,1,1},{2,2,2}};
-
- byte[,,] bArray1 = {{{1,2},{3,4}},{{5,6},{7,8}}};
-
- Write_2DArray(sArray1);
-
- Write_2DArray(new short[,]{{1,1,1},{2,2,2}});
-
-
- int[][] JagIntArray1;
-
- JagIntArray1 = new int [2][] {
- new int[]{1,2},
- new int[]{3,4,5,6}
- };
- JagIntArray1 = new int [][]{
- new int[]{1,2},
- new int []{3,4,5},
- intArray2
- };
-
- int[][] JagIntArray2 = {
- new int[]{1,1,1},
- new int []{2,2},
- intArray1
- };
-
- Write_JagArray(JagIntArray1);
- }
- private static void Write_1DArray(int[] ArrayName)
- {
-
- for (int i=0;i<ArrayName.Length ;i++)
- {
- Console.Write(ArrayName[i]+" ");
- }
- Console.WriteLine ();
- }
- private static void Write_1DArray(string[] ArrayName)
- {
- for (int i=0;i<ArrayName.Length;i++)
- {
- Console.Write(ArrayName[i]+" ");
- }
- Console.WriteLine ();
- }
- private static void Write_2DArray(short[,] ArrayName)
- {
- for (int i=0; i<ArrayName.GetLength ;i++)
- {
- Console.Write("二维数组第{0}行:",i+1);
- for (int j=0;j<ArrayName.GetLength(1);j++)
- {
- Console.Write (ArrayName[i,j]+" ");
- }
- Console.WriteLine ();
- }
- }
- private static void Write_JagArray(int[][] ArrayName)
- {
- for (int i=0;i<ArrayName.Length;i++)
- {
- Console.Write("交错数组第{0}个子数组:",i+1);
-
- for (int j=0;j<ArrayName[i].Length ;j++)
- {
- Console.Write (ArrayName[i][j]+" ");
- }
- Console.WriteLine();
- }
- }
- }
- }