《C#数据结构与算法》–2020 最新精讲版:2-3 获取、修改、打印 笔记

本文是《C#数据结构与算法》2020年最新精讲版的笔记,主要涵盖了获取、修改和打印数组元素的内容。通过学习,包括回顾、创建新数组、重写ToString方法以正确显示数组,以及测试其他函数和元素的获取与修改操作。
摘要由CSDN通过智能技术生成

《C#数据结构与算法》–2020 最新精讲版:2-3 获取、修改、打印 笔记



学习视频

《C#数据结构与算法》–2020 最新精讲版:2-3 获取、修改、打印


一、回顾

创建新类Array1

代码如下:

        private int[] data;
        private int N;
        
        public Array1(int capacity)
        {
            data = new int[capacity];
            N = 0;
        }

        public Array1() : this(10) { }
        //与 public Array1() : this(10) { }
        //功能相同
        //public Array1()
        //{
        //    data = new int[10];
        //    N = 0;
        //}

        public int capacity//访问数组的容量
        {
            get { return data.Length; }
        }

        public int count//访问动态数组的实际存储元素的多少
        {
            get { return N; }
        }

        public bool IsEnmpty//判断访问是否为空
        {
            get { return N == 0; }
        }
        
        //在位置index添加元素e
        public void Add(int index, int e)
        {
            if (index < 0 || index > N)
            {
                throw new ArgumentException("数组索引越界");
            }

            if (N == data.Length)
            {

                throw new ArgumentException("数组已满");
            }

            for (int i = N - 1; i >= index; i--)
                data[i + 1] = data[i];

            data[index] = e;
            N++;
        }
        
        //在末尾添加数据e
        public void AddLast(int e)
        {
            Add(N, e);
        }
        
        //在首位添加数据e
        public void AddFirst(int e)
        {
            Add(0, e);
        } 

二、创建新数组

代码如下:

            Array1 a = new Array1(20);
            for(int i=0;i<10;i++)
            {
                a.AddLast(i);
            }
            Console.WriteLine(a);
            Console.Read();

此时的运行结果为:

array.Array1

解决方案为:在类Array1中重写ToString方法

代码如下:

        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            res.Append(string.Format("Array1:count={0} capacity={1}\n", N, data.Length));
            res.Append("[");
            for (int i = 0; i < N; i++)
            {
                res.Append(data[i]);
                if (i != N - 1)
                    res.Append(",");
            }
            res.Append("]");
            return res.ToString();
        }

此时的运行结果为:

Array1:count=10 capacity=20
[0,1,2,3,4,5,6,7,8,9]

三、其他函数测试

代码如下:

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

namespace array
{
    class Program
    {
        static void Main(string[] args)
        {
            Array1 a = new Array1(20);
            for(int i=0;i<10;i++)
            {
                a.AddLast(i);
            }
            Console.WriteLine(a);
            Console.Read();

            //在首位添加元素 e=66
            a.AddFirst(66);
            Console.WriteLine(a+ "\r\n");

            //在末尾添加元素 e=77
            a.AddLast(77);
            Console.WriteLine(a + "\r\n");

            //在位置添加元素 e=11
            a.Add(7,11);
            Console.WriteLine(a + "\r\n");
            Console.ReadKey();
        }
    }
}

此时的运行结果为

Array1:count=10 capacity=20
[0,1,2,3,4,5,6,7,8,9]

Array1:count=11 capacity=20
[66,0,1,2,3,4,5,6,7,8,9]

Array1:count=12 capacity=20
[66,0,1,2,3,4,5,6,7,8,9,77]

Array1:count=13 capacity=20
[66,0,1,2,3,4,5,11,6,7,8,9,77]

四、元素的获取和修改

在类中编写程序:

代码如下:

        //指定位置查询
        public int Get(int index)
        {
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");
            return data[index];
        }

        //查询数组头部元素的值
        public int GetFirst()
        {
            return Get(0);
        }
        //查询数组尾部元素的值
        public int GetLast()
        {
            return Get(N - 1);
        }

        //元素的修改
        //将index位置的元素改为newE
        public void Set(int index, int newE)
        {
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");
            data[index] = newE;
        }

并在Program中进行调用测试

            Console.WriteLine(a.GetFirst());
            Console.WriteLine(a.GetLast());
            Console.WriteLine(a.Get(5));
            Console.ReadKey();

            a.Set(5, 99);
            Console.WriteLine(a);
            Console.ReadKey();

此时的运行结果为:

Array1:count=10 capacity=20
[0,1,2,3,4,5,6,7,8,9]

Array1:count=11 capacity=20
[66,0,1,2,3,4,5,6,7,8,9]

Array1:count=12 capacity=20
[66,0,1,2,3,4,5,6,7,8,9,77]

Array1:count=13 capacity=20
[66,0,1,2,3,4,5,11,6,7,8,9,77]

66
77
4
Array1:count=13 capacity=20
[66,0,1,2,3,99,5,11,6,7,8,9,77]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值