数组

本文详细探讨了数组这一基本数据结构,从定义、类型、内存分配到操作方法,全面解析了数组的工作原理。通过实例分析,展示了数组在存储和检索大量数据时的优势,并讨论了数组在实际编程中的常见应用和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

namespace Beas05
{
    class Program
    {
        static void Main(string[] args)
        {
            //数据类型[] 数组名 = new 数据类型[数组长度];
            //int[] ints = new int[5];
            //数组索引 从0开始 对数组的数组元素进行赋值从0开始
            //ints[0] = 1;
            //ints[1] = 2;
            //ints[2] = 3;
            //ints[3] = 4;
            //ints[4] = 5;
            //数据类型[] 数组名 = { 值1,值2,值3,值4 };
            //int[] ints = { 1, 2, 3, 4, 5 };
            //Console.WriteLine(ints.Length);

            //数据类型[,] 数组名 = new 数据类型[数值1, 数值2];
            //int[5, 10] 5行10列(每一行10个列)
            //所以的数据类型  都会有默认值  值类型 关键字 蓝色

            //int[,] ints = new int[5,10];
            //ints[0, 0] = 1;
            //ints[0, 1] = 2;
            //Console.WriteLine(ints[3,4]);
            //数据类型[,] 数组名 = new 数据类型[,] { { 值1,值2 }, { 值1,值2 }, { 值1,值2 } };
            //int[,] ints = new int[,] { { 1, 1 }, { 2, 2 },{ 3, 3 }, { 4, 4 } };
            //Console.WriteLine(ints[3,1]);



            //遍历数组的方式
            //for
            //int[] ints = { 100, 200, 300, 400, 500 };
            //for (int i = 0; i < ints.Length; i++)
            //{
            //    Console.WriteLine(ints[i]);
            //}
            //Console.WriteLine("----------------------");
            //foreach(元素类型 item in 集合或者数组名)
            //是根据迭代  循环一次 判断是否存在 如果存在着获取下一个信息
            //    foreach (int item in ints)
            //    {
            //        Console.WriteLine(item);
            //    }


            //删除数组中的元素
            //int[] ints = { 100, 200, 300, 400, 500 };
            //Console.WriteLine("请输入你要删除数组的第几个元素");
            // int index = Int32.Parse(Console.ReadLine());
            //for (int i =index; i < ints.Length-index; i++)
            //{
            //    ints[i] = ints[i + 1];
            //}

            //将最后一位数字代替为0;
            //ints[index] = 0;
            //  Console.WriteLine("--------------------");
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //冒泡排序
            //int [] ints= { 700, 200, 300, 400, 500 };
            //for (int i = 0; i < ints.Length-1; i++)
            //{
            //    for (int j = 0; j < ints.Length-1-i; j++)
            //    {
            //        if (ints[j] > ints[j + 1]) {
            //            int temp = ints[j];
            //            ints[j] = ints[j + 1];
            //            ints[j + 1] = temp;

            //        }
            //    }
            //}
            //Console.WriteLine("----------------");
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //Sort方法用于对一维Array数组中的元素进行排序。
            //int[] ints = { 700, 200, 300, 400, 500 };
            //Array.Sort(ints);
            //Console.WriteLine("----------------");
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}
            //  Reverse方法用于反转一维数组元素的顺序。但是不会进行排
            //int[] ints = { 700, 200, 300, 400, 500 };
            //Array.Sort(ints);
            //Array.Reverse(ints);
            //Console.WriteLine("----------------");
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //合并数组
            //int[] array1 = { 1, 2, 3, 4, 5 };
            //int[] array2 = { 6,7,8,9,10 };
            //int[] array3 = new int[array1.Length + array2.Length];
            //for (int i = 0; i < array3.Length; i++)
            //{
            //    if (i < array1.Length)
            //    {
            //        array3[i] = array1[i];
            //    }
            //    else {
            //        array3[i] = array2[i - array1.Length];
            //    }
            //}
            //foreach (int item in array3)
            //{
            //    Console.WriteLine(item);
            //}


            // 拆分数组
            //int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
            //int index = 0;
            //if (ints.Length % 2 != 0)
            //{
            //    index = ints.Length % 2;
            //}
            //int[] int1 = new int[ints.Length / 2];
            //int[] int2;
            //if (index > 0)
            //{
            //    int2 = new int[(ints.Length / 2) + index];
            //}
            //else
            //{
            //    int2 = new int[ints.Length / 2];
            //}

            //for (int i = 0; i < ints.Length; i++)
            //{
            //    if (i < int1.Length)
            //    {
            //        int1[i] = ints[i];
            //    }
            //    else
            //    {
            //        int2[i - int1.Length] = ints[i];
            //    }

            //}
            //Console.WriteLine("----------------------------");
            //foreach (int item in int1)
            //{
            //    Console.WriteLine(item);
            //}
            //Console.WriteLine("----------------------");
            //foreach (int item in int2)
            //{
            //    Console.WriteLine(item);
            //}
            //Console.ReadLine();

            //二维棋盘

            // 1.定义一个长度为5的字符串数组。循环输入5个姓名。然后再输入一个用户的姓名,检查数组中是否有这个人的姓名,并输出所在下标。
            //string[] name = { "a", "bb", "ccc", "dd", "ee" };
            //Console.WriteLine("请输入姓名");
            //string s= Console.ReadLine();
            //for (int i = 0; i < name.Length; i++)
            //{
            //    if (s.Equals(s[i])) {
            //        Console.WriteLine(name[i]);
            //    }
            //}

            //2.定义一个长度为5的整数数组,循环赋值,根据用户输入的位数,进行对应的删除
            //int[] ints = { 1, 2, 3, 4, 5 };
            //Console.WriteLine("请输入你要删除的下标");
            //int index = Int32.Parse(Console.ReadLine());
            //for (int i = 0; i < ints.Length-index; i++)
            //{
            //    ints[i] = ints[i + 1];
            //}
            //ints[index] = 0;
            //Console.ReadKey();

            //3.定义一个长度为7的字符串数组,并为其赋值,将该数组的值进行反转
            //string[] s = new string[7];
            //for (int i = 0; i < s.Length; i++)
            //{
            //    s[i] = i + "";

            //}
            //Array.Sort(s);
            //foreach (string item in s)
            //{
            //    Console.WriteLine(item);
            //}
            //4.99乘法表
            //for (int i = 1; i < 10; i++)
            //{
            //    for (int j= 0; j <=i; j++)
            //    {
            //        Console.WriteLine("{0}*{j}={2}\t",j,i,i*j);
            //    }
            //}
            Console.ReadKey();
            //5.冒泡循环
            //int[] ints = { 1, 23, 11, 24, 43 };
            //for (int i = 0; i < ints.Length; i++)
            //{
            //    for (int j = 0; j < ints.Length-i; j++)
            //    {
            //        if (ints[j] < ints[j + 1]) {
            //            int temp = ints[i];
            //            ints[j] = ints[j+1];
            //            ints[j+1] = temp;
            //        }
            //    }
            //}

            //二维棋盘
            int[,] m = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            for (int i = 0; i < (m.Length/2)-1; i++)
            {
                for (int j = 0; j < (m.Length/2)-1; j++)
                {
                    Console.WriteLine(m[i,j]);
                }
            }
            Console.ReadKey();
        }

    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值