(原创)C#零基础学习笔记004-数组

微信公众号已开通,搜索微信公众号:程序喵星人。点击关注^_^

4.数组

  所需掌握的知识点:

    1.数组的定义;

    2.声明和操作一维和二维数组;

    3.交错数组和多维数组;

    4.简单的排序方法;

    5.数组列表、哈希表、堆栈集合的用法;

    6.静态数组的扩容和缩容的操作;

  1.数组定义

    数组是 同一个数据类型 的一组值。

    数组属于 引用类型,因此存储在堆内存中。

    数组元素初始化或给数组元素赋值都可以在声明数组时或在程序的后面阶段中进行。

    语法:

      数据类型[元素个数] 数组名称;

      int[6] arrayInts;

    数组下标,从 0 开始;

    比如:

      学生分数的整数数组(5个):arr[0] = 78; arr[1] = 67; arr[2] = 89; arr[3] = 92; arr[4] = 66;

      职员姓名的字符串数组(5个):arr[0] = "Joe"; arr[1] = "Tom"; arr[2] = "Lee"; arr[3] = "Jim"; arr[4] = "Bill";

      温室的浮点数组(5个):arr[0] = 23.5;arr[1] = 18.9; arr[2] = 27.3; arr[3] = 21.4; arr[4] = 29.6;

    案例:

      1.定义一个数组存储学生的分数,并输出学生的分数。

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

// 数组定义

namespace Lesson_16_1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 定义数组,先规定数组存储的类型
            // 第一种定义方式:在定义数组时,给数组赋值
            int[] intScores = {30, 60, 90, 100, 120};
            for (int i = 0; i < intScores.Length; ++i) {  // 下标从 0 开始
                Console.WriteLine(intScores[i]);
            }
            // 第二种定义方式:在定义数组时,只规定数组长度,不赋值
            string[] strNames = new string[3];  // 定义长度为3
            strNames[0] = "First";  // 下标从 0 开始
            strNames[1] = "Second";
            strNames[2] = "Three";
            for (int i = 0; i < strNames.Length; ++i) {
                Console.WriteLine(strNames[i]);
            }

        }
    }
}

  

  2.一维数组

    int[] array2 = { 1, 3, 5, 7, 9 };

    string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

    声明一个数组变量但不将其初始化。

      int[] array3;

      array3 = new int[] { 1, 3, 5, 7, 9 }; // ok

    案例应用:

      1.编写应用程序,对数组中的数字求和{ 23, 67, 35, 24, 67 };

      2.编写程序,把由 10 个元素组成的一维数组倒序输出。如{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

      3.求以下数组中的最大值和最小值{ 23, 67, 35, 24, 67 };

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

// 一维素组

namespace Lesson_17_1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            // example_1: 求和
            int[] intNums = new int[]{ 2, 8, 9, 7 };  // 数组:第三种定义方式:
            int sum = 0;
            for (int i = 0; i < intNums.Length; ++i)
            {
                sum += intNums[i];
            }
            Console.WriteLine("数字之和:{0}", sum);
            */

            /*
            // example_2: 倒序输出
            int[] intS = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            for (int i = intS.Length - 1; i >= 0; --i)
            {
                Console.WriteLine(intS[i]);
            }
            */

            // example_3: 输出最大数和最小数
            int[] intN = {8, 96, 2, 45, 23};
            int max = intN[0], min = intN[0];
            for (int i = 0; i < intN.Length; ++i)
            {
                if (intN[i] > max)
                {
                    max = intN[i];
                }
                if (intN[i] < min)
                {
                    min = intN[i];
                }
            }
            Console.WriteLine("最大值:{0},最小值:{1}", max, min);
        }
    }
}

  

  3.多维数组

    多维数组又称矩阵数组。

    二维数组声明:

      dataType[ , ] names;

    三维数组声明:

      dataType[ , , ] names;

    二维数组

      多维数组最简单的形式是二维数组。

      一个二维数组可以被认为是带有 x 行 和 y 列的表格。

      下面是一个二维数组,包含 3 行( R: Row),4列( C: Colunm);

        C0 C1 C2 C3

      R0 a[0][0] a[0][1] a[0][2] a[0][3]

      R1 a[1][0] a[1][1] a[1][2] a[1][3]

      R2 a[2][0] a[2][1] a[2][2] a[2][3]

      因此,数组中的每个元素是使用形式为 a[ i , j ] 的元素名称来标识的,其中 a 是数组名称,i 和 j 是唯一标识 a 中每个元素的下标。

      初始化二维数组

        int [,] a = new int [3,4] {

          {0, 1, 2, 3} , /* 初始化索引号为 0 的行 */

          {4, 5, 6, 7} , /* 初始化索引号为 1 的行 */

          {8, 9, 10, 11} /* 初始化索引号为 2 的行 */

        };

      访问二维数组元素

        int val = a[2,3];

        上面的语句将获取数组中第 3 行第 4 个元素。

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

// 多维数组--二维数组

namespace Lesson_17_2  // 多维数组
{
    class Program
    {
        static void Main(string[] args)
        {
            // example_1: 输出多维数组中每个元素的值
            int[,] a = new int[5, 2] { { 0, 1 }, { 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 } };
            for ( int i = 0; i < 5; ++i )
            {
                for (int j = 0; j < 2; ++j)
                {
                    Console.WriteLine( "a[{0}, {1}] = {2}", i, j, a[i, j] );
                }
            }
            Console.ReadKey();
        }
    }
}

  

  4.数组的缩容和扩容操作

    a.扩容

      在原有的定长的数组中增加长度。需要将之前的数据复制到新增的数组中。

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

// 数组扩容

namespace Lesson_18_1
{
    class Program
    {
        static void Main(string[] args)
        {
            // example_1: 数组的扩容
            int[] arrS = new int[4] { 12, 13, 14, 15 };
            int[] tmp = new int[arrS.Length + 1];
            for ( int i = 0; i < arrS.Length; ++i )
            {
                tmp[i] = arrS[i];
            }
            Console.WriteLine( "输入新增的元素的数据:" );
            int addNum = Convert.ToInt32( Console.ReadLine() );
            tmp[tmp.Length - 1] = addNum;
            // 改变原有的数组的指向地址
            arrS = tmp;
            Console.WriteLine( "扩容之后的数据" );
            for ( int i = 0; i < arrS.Length; ++i )
            {
                Console.WriteLine( arrS[i] );
            }
        }
    }
}

  

    b.缩容 

      在原有的数组中减少其中的元素,而其余元素的大小并不发生改变。

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

// 数组缩容

namespace Lesson_19_1
{
    class Program
    {
        static void Main(string[] args)
        {
            // example_1: 数组缩容
            int[] arrS = new int[4] { 12, 13, 14, 15 };
            Console.WriteLine( "请输入你想要删除的元素:" );
            int intGetNum = Convert.ToInt32( Console.ReadLine() ), intGetIndex = -1;
            for ( int i = 0; i < arrS.Length; ++i )
            {
                if ( intGetNum == arrS[i] )
                {
                    intGetIndex = i;
                    break;
                }
            }
            if ( intGetIndex >= 0 )
            {
                int[] tmp = new int[arrS.Length - 1];
                for ( int i = 0; i < tmp.Length; ++i )
                {
                    if ( i >= intGetIndex )
                    {
                        tmp[i] = arrS[i + 1];
                    }
                    else
                    {
                        tmp[i] = arrS[i];
                    }
                }
                arrS = tmp;
                Console.WriteLine( "数组缩容后的元素为:" );
                for ( int i = 0; i < arrS.Length; ++i )
                {
                    Console.WriteLine( arrS[i] );
                }
            }
            else
            {
                Console.WriteLine( "没有找到你想要删除的元素" );
            }
        }
    }
}

  

  5.数组冒泡排序法

    将待排序的元素看作是竖着排列的“气泡”,较小的气泡比较轻,从而要往上浮。

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

// 数组冒泡排序

namespace Lesson_20_1
{
    class Program
    {
        static void Main(string[] args)
        {
            // example_1: 数组冒泡排序
            int[] intS = { 9, 6, 12, 3, 7, 8 };
            int tmp = 0;
            for ( int i = 1; i < intS.Length; ++i )  // 比较的趟数, 从 1 开始
            {
                for ( int j = 1; j <= intS.Length - i; ++j )  // 每一趟比较的次数, 从 1 开始
                {
                    if ( intS[j - 1] > intS[j] )
                    {
                        tmp = intS[j - 1];
                        intS[j - 1] = intS[j];
                        intS[j] = tmp;
                    }                   
                }
            }
            // 会发现,上面的:趟数 i 和 次数j的最大值(intS.Length - i)两者之和为:数组的长度( i + ( intS.Length - i ) == intS.Length )。这个是冒泡排序的一个潜在的原则。
            Console.WriteLine( "数组排序之后的元素为:" );
            for ( int i = 0; i < intS.Length; ++i )
            {
                Console.WriteLine( intS[i] );
            }
            Console.ReadKey();  // 避免一闪而过
        }
    }
}

  

  6.数组列表集合

    数组列表集合 ArrayList。

    类似于一维数组。

    数组列表是动态数组。

    可以存放任何对象。

    常用方法:

      增加元素-Add

      插入元素-Insert

      删除元素-Remove

    案例练习

      创建一个程序,在程序中添加 5 名同学,并任意随机抽取两名同学出来。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;  // 表示引入集合的命名空间

// 数组列表集合ArrayList

namespace Lesson_21_1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            // example_1
            // 声明一个 ArrayList 的对象
            ArrayList arrList = new ArrayList();
            // 可以在这个数组中任意添加元素
            arrList.Add( 12 );
            arrList.Add( 5 );
            arrList.Add( 3 );
            arrList.Add( 9 );
            arrList.Add( 10 );
            Console.WriteLine( "数组的元素数量是:" + arrList.Count );
            // 每个放到 ArrayList 里的元素都会转换为 object 类型存放
            foreach ( object obj in arrList )  // foreach固定语法,获得每个元素
            {
                Console.WriteLine( obj );  // 输出元素
            }
            // Console.WriteLine( "排序之后的ArrayList的元素是:" );
            // arrList.Sort();  // ArrayList 自带排序接口
            // foreach ( object obj in arrList )
            // {
            //     Console.WriteLine( obj );
            // }
            // 向 ArrayList 里面插入元素
            arrList.Insert( 0, 78 );  // 插入的元素,放在 0 索引的元素之前
            Console.WriteLine( "插入元素之后的数据是:" );
            foreach ( object obj in arrList )
            {
                Console.WriteLine( obj );
            }
            // 删除 ArrayList 里的元素
            arrList.Remove( 78 );   // 通过 值Value 删除
            arrList.RemoveAt( 0 );  // 通过 索引Index 删除
            Console.WriteLine( "删除元素之后的数据是:" );
            foreach ( object obj in arrList )
            {
                Console.WriteLine( obj );
            }
            */

            // example_2: 添加 5 名同学,并随机抽取 2 名同学出来
            ArrayList arrList = new ArrayList();
            arrList.Add( "李一" );
            arrList.Add( "李二" );
            arrList.Add( "李三" );
            arrList.Add( "李四" );
            arrList.Add( "李五" );
            ArrayList arrChou = new ArrayList();  // 把抽取的学生放入到该集合中
            for ( int i = 1; i <= 2; ++i )
            {
                while ( true )
                {
                    int randIndex = new Random().Next( 0, 5 );  // 随机数,前包后不包[0, 5 )
                    if ( arrChou.Contains( arrList[randIndex] ) )  // 如果是重复抽取
                    {
                        continue;
                    }
                    else
                    {
                        arrChou.Add( arrList[randIndex] );
                        break;
                    }
                }
            }
            foreach ( object obj in arrChou )
            {
                Console.WriteLine( obj );
            }
            Console.ReadKey();
        }
    }
}

  

原文地址:https://www.cnblogs.com/wodehao0808/p/14272840.html