C#OOP篇--数组

本文介绍了C#中的数组概念,包括数组的定义、类型和长度,以及在图像处理中的应用。讨论了一维和二维数组的起始下标,并提到了由于数组固定长度的限制,导致在插入元素时的不便,进而引出了ArrayList作为解决方案。
摘要由CSDN通过智能技术生成

1.数组概念

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

数组是一种数据类型,并且二维数组在图像处理中会应用。一维数组的起始下标是[0]。二维数组的起始下标是[0,0]。交错也称参差数组的起始下标是[0][0]。

数组一定是固定长度和类型确定并且有序的,这种呆板的数据类型,导致它的INSERT,非常不方便,于是有了ArrayList

2.示例代码

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

namespace ShuZu
{
    class Program
    {
        static void Main(string[] args)
        {
          
                //System.Array
                //1、数组[]特定类型、固定长度
                string[] str1 = new string[3];
                str1[0] = "a";
                str1[1] = "b";
                str1[2] = "c";
                Console.WriteLine(str1[2]);

                string[] str2 = new string[] { "a", "b", "c" };
                Console.WriteLine(str2[0]);

                string[] str3 = { "a", "b", "c" };
                Console.WriteLine(str3[0]);

                //2.二维数组
                //int[,] intArray = new int 
                int[,] intArray = new int[2, 3];
                intArray[0, 0] = 1;
                intArray[0, 1] = 11;
                intArray[0, 2] = 111;
                intArray[1, 0] = 2;
                intArray[1, 1] = 22;
                intArray[1, 2] = 222;
                Console.WriteLine("{0},{1},{2}", intArray[0, 0], intArray[0, 1], intArray[0, 2]);
                Console.WriteLine("{0},{1},{2}", intArray[1, 0], intArray[1, 1], intArray[1, 2]);

                //3多维数组
                int[,,] intArray1 = new int[,,]
                {
               {{1,1},{11,11},{111,111}},
               {{2,2},{22,22},{33,33}},
               {{3,3},{33,33},{333,333}}
                };
                Console.WriteLine("多维数组");
                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]);
                Console.WriteLine("{0},{1},{2},{3}", intArray1[1, 0, 0], intArray1[1, 0, 1], intArray1[1, 1, 0], intArray1[1, 1, 1]);
                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]);

                //4交错数组即数组的数组
                int[][] intArray2 = new int[4][];
                intArray2[0] = new int[] { 1 };
                intArray2[1] = new int[] { 2, 22 };
                intArray2[2] = new int[] { 3, 33, 333 };
                intArray2[3] = new int[] { 4, 44, 444, 4444 };
                Console.WriteLine("交错数组");
                for (int i = 0; i < intArray2.Length; i++)
                {
                    for (int j = 0; j < intArray2[i].Length; j++)
                    {
                        Console.WriteLine("{0}", intArray2[i][j]);
                    }
                }
                Console.ReadKey();
                int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
                Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };
                Console.WriteLine("Initially,");
                Console.Write("integer array:");
                PrintValues(myIntArray);
                Console.Write("Object array: ");
                PrintValues(myObjArray);

                System.Array.Copy(myIntArray, myObjArray, 2);

                Console.WriteLine("\n After copying the first two elements of the integer array to the Object array.");
                Console.Write("integer array:");
                PrintValues(myIntArray);
                Console.Write("Object array: ");
                PrintValues(myObjArray);

                System.Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2);

                Console.WriteLine("\nAfter copying the last two elements of the object array to the integer array,");
                Console.Write("integer array:");
                PrintValues(myIntArray);
                Console.Write("Object array:");
                PrintValues(myObjArray);
                Console.ReadKey();
            }

            public static void PrintValues(Object[] myArr)
            {
                foreach (Object i in myArr)
                {
                    Console.Write("\t{0}", i);
                }
                Console.WriteLine();
            }

            public static void PrintValues(int[] myArr)
            {
                foreach (int i in myArr)
                {
                    Console.Write("\t{0}", i);
                }
                Console.WriteLine();
            }
        }
    }

3.演示效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值