设为首页 加入收藏

TOP

C# 数组Array(一)
2019-09-17 18:57:14 】 浏览:63
Tags:数组 Array

数组是对相同类型的一组数据的封装。数组定义的时候,要说明是对哪一种类型的封装,并且要指定长度。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace TestArrayList
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             //System.Array
 13             //1、数组[]特定类型、固定长度
 14             string[] str1 = new string[3];
 15             str1[0] = "a";
 16             str1[1] = "b";
 17             str1[2] = "c";
 18             Console.WriteLine(str1[2]);
 19 
 20             string[] str2 = new string[] { "a", "b", "c" };
 21             Console.WriteLine(str2[0]);
 22 
 23             string[] str3 = { "a", "b", "c" };
 24             Console.WriteLine(str3[0]);
 25 
 26             //2.二维数组
 27             //int[,] intArray = new int 
 28             int[,] intArray = new int[2, 3];
 29             intArray[0, 0] = 1;
 30             intArray[0, 1] = 11;
 31             intArray[0, 2] = 111;
 32             intArray[1, 0] = 2;
 33             intArray[1, 1] = 22;
 34             intArray[1, 2] = 222;
 35             Console.WriteLine("{0},{1},{2}", intArray[0, 0], intArray[0, 1], intArray[0, 2]);
 36             Console.WriteLine("{0},{1},{2}", intArray[1, 0], intArray[1, 1], intArray[1, 2]);
 37 
 38             //3多维数组
 39             int[, ,] intArray1 = new int[,,]
 40             {
 41                {{1,1},{11,11},{111,111}},
 42                {{2,2},{22,22},{33,33}},
 43                {{3,3},{33,33},{333,333}}
 44             };
 45             Console.WriteLine("多维数组");
 46             Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[0, 0, 0], intArray1[0, 0, 1], intArray1[0, 1, 0], intArray1[0, 1, 1], intArray1[0, 2, 0], intArray1[0, 2, 1]);
 47             Console.WriteLine("{0},{1},{2},{3}", intArray1[1, 0, 0], intArray1[1, 0, 1], intArray1[1, 1, 0], intArray1[1, 1, 1]);
 48             Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[2, 0, 0], intArray1[2, 0, 1], intArray1[2, 1, 0], intArray1[2, 1, 1], intArray1[2, 2, 0], intArray1[2, 2, 1]);
 49 
 50             //4交错数组即数组的数组
 51             int[][] intArray2 = new int[4][];
 52             intArray2[0] = new int[] { 1 };
 53             intArray2[1] = new int[] { 2, 22 };
 54             intArray2[2] = new int[] { 3, 33, 333 };
 55             intArray2[3] = new int[] { 4, 44, 444, 4444 };
 56             Console.WriteLine("交错数组");
 57             for (int i = 0; i < intArray2.Length; i++)
 58             {
 59                 for (int j = 0; j < intArray2[i].Length; j++)
 60                 {
 61                     Console.WriteLine("{0}", intArray2[i][j]);
 62                 }
 63             }
 64             Console.ReadKey();
 65             int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
 66             Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };
 67             Console.WriteLine("Initially,");
 68             Console.Write("integer array:");
 69             PrintValues(myIntArray);
 70             Console.Write("Object array: ");
 71             PrintValues(myObjArray);
 72 
 73             System.Array.Copy(myIntArray, myObjArray, 2);
 74 
 75             Console.WriteLine("\n After copying the first two elements of the integer array to the Object array.");
 76             Console.Write("integer array:");
 77             PrintValues(myIntArray);
 78             Console.Write("Object array: ");
 79             PrintValues(myObjArray);
 80 
 81             System.Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2);
 82 
 83             Console.WriteLine("\nAfter copying the last two elements of the object array to the integer array,");
 84             Console.Write("integer array:");
 85             PrintValues(myIntArray);
 86             Console.Write("Object array:");
 87             PrintValues(myObjArray);
 88             Console.ReadKey();
 89         }
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C# 委托基础1.0 下一篇使用 MSIX 打包 DotNetCore 3.0 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目