C#数组

1.数组的概念

数组是一个存储相同类型元素的固定大小的顺序集合。

所有的数据都是由连续的内存位置组成

可以通过索引来访问数组中的元素

2.数组的声明和初始化

(1)声明

datatype指数组的类型,[]可以指定数组的纬度,arrayName指数组的名称

datatype[] arrayName;

(2)初始化

数组是一个引用类型,需要使用 new 关键字来创建数组的实例

string [] Students=new string[3]

(3)赋值给数组

string[] Students = new string[3] { "kaven", "melon", "lucy" };

(4)遍历数组

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

namespace 数组
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Students = new string[3] { "kaven", "melon", "lucy" };

            //for (int i = 0; i < Students.Length; i++)
            //{
            //    Console.WriteLine(Students[i]);

            //}
            Console.WriteLine("学生列表:");
            Console.WriteLine("++++++++++++++++++++++++++");
            foreach (string s in Students) {
                Console.WriteLine(s);
            }

            Console.WriteLine("++++++++++++++++++++++++++");
            //通过索引访问数组元素
            Console.WriteLine("第三个学生是:" + Students[2]);
            Console.ReadKey();


        }
    }
}

这里写图片描述

3.二维数组

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

namespace 数组
{
    class Program
    {
        static void Main(string[] args)
        {
        int[,] number = new int[5, 2]{
                {0,0},
                {1,1},
                {2,4},
                {3,9},
                {4,16}
            };
            Console.WriteLine("++++++++++++++++++");
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 2; j++) {

                    Console.WriteLine("第{0}行{1}列为{2}",i,j,number[i,j]);

                }
            }
            Console.WriteLine("++++++++++++++++++");
                Console.ReadKey();


        }
    }
}

这里写图片描述

4.数组参数

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

namespace 数组
{
    class Program
    {
        public static int getMultiple(int[] arr)
        {
            int result = 1;
            foreach (int item in arr)
            {
                result = result * item;
            }
            return result;
        }
        static void Main(string[] args)
        {
          // 求乘积
            int[] number = new int[5] { 1, 3, 5, 7, 9 };
            Console.Write("数组元素");
            foreach (int i in number) {
                Console.Write(i+"、");
            }
            Console.Write("的乘积是");
            Console.Write(getMultiple(number));
            Console.ReadKey();
              }

    }
}

这里写图片描述

5.参数数组

参数数组通常用于传递未知数量的参数给函数。

格式

public 返回类型 方法名称( params 类型名称[] 数组名称 )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 数组
{
    class Program
    {
      public static double getSum(params double[] arr) {
            double sum = 0;
            for (int i = 0; i < arr.Length; i++) {
                sum += arr[i];
            }
            return sum;
        }
        static void Main(string[] args)
        {
            Console.WriteLine(getSum(1.2, 2.3, 3.4, 4.5));
            Console.ReadKey();
                    }

    }
}

结果

这里写图片描述

把 params关键字去掉就会报错

这里写图片描述

6.Array

Array 类提供了各种用于数组的属性和方法,是所有数组的基类

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

namespace 数组
{
    class Program
    { 
    static void Main(string[] args)
            int[] a1 = { 1, 2, 3, 4, 5, 6, 7, 8 };
            int[] a2=new int[5];
            //数组a1从第一个元素开始复制5个元素到数组a2
            Array.Copy(a1, a2, 5);
            Console.WriteLine("数组a2:");
            foreach (int i in a2) {
                Console.Write(i+" ");
            }
            Console.WriteLine();
            //
            int[] original = new int[] { 78, 12, 39, 90, 64, 56, 30, 2, 7 };
            int[] temp = original;
            //原始数组

            Console.WriteLine("原始数组");
            foreach (int i in original)
            { 
                Console.Write(i + " ");
            }
            Console.WriteLine();
            //逆转数组
            Array.Reverse(temp);
            Console.WriteLine("逆转数组");
            foreach (int i in temp)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            //排序数组
            Array.Sort(temp);
            Console.WriteLine("排序数组");
            foreach (int i in temp)
            {

                Console.Write(i + " ");
            }

            Console.ReadKey();
        }

    }
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值