总结
1.视频资源P2-P8:
https://www.bilibili.com/video/BV1gE41157pC?p=8&spm_id_from=pageDriver
2.学习内容:
1)静态数组与动态数组 P2;
2)创建一个可实现返回数组基本信息、增、删、查、改、动态扩容/缩容、存储任意类型元素的数组类。
3.2021/10/08回溯:
紫色红色笔均为回溯后增加
1)根据伪代码编写程序;
2)发现了P4存在的错误,注意一下。
具体内容
P2 数组
1、静态数组&动态数组;
2、简单举例比较两者区别;
3、P7装箱与拆箱中,进行了动态数组性能测试,并进行了优劣比较;
4、创建数组类–实现返回数组基本信息的功能。
P3 插入
1、创建数组类–实现向数组中增加元素的基本功能;
2、我写的蹩脚的伪代码,后面我会再自己一一把功能实现程序写一遍。
注意红笔处的更改
P5 包含、查找、删除
1、创建数组类–实现查找(某元素所在位置)、删除(数组中某位置元素)的基本功能;
2、我写的蹩脚的伪代码,后面我会再自己一一把功能实现程序写一遍。
P6 动态数组
1、创建数组类–实现数组动态扩容/缩容的基本功能;
2、我写的蹩脚的伪代码,后面我会再自己一一把功能实现程序写一遍。
P7 装箱与拆箱、P8 使用泛型
1、拆箱与装箱的概念;
2、比较泛型与非泛型数组存储值类型/引用类型数据的性能;
3、创建数组类–实现在数组中存储任意类型元素的功能。
完整代码块
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Array1<E>
{
private E[] data;
//数组中实际存储数据个数
private int N;
//明确传入参数的个数
public Array1(int capacity)
{
data = new E[capacity];
N = 0;
}
//不确定传入参数的个数
public Array1()
{
//设置一个默认容量
data = new E[10];
N = 0;
}
#region 返回数组容量、元素个数、判断数组是否为空
//返回数组容量
public int Capacity
{
get { return data.Length; }
}
//返回数组中元素个数
public int Count
{
get { return N; }
}
//判断数组是否为空
public bool IsEmpty
{
get { return N == 0; }
}
#endregion
# region 向数组中插入元素
//向数组中插入元素
public void Insert(int index,E e)
{
if (index < 0 || index > N)
{
throw new ArgumentException("数组索引越界");
}
if (N == data.Length)
{
RestCapacity(2 * data.Length);
}
for (int i = N-1; i >= index; i--)
{
data[i + 1] = data[i];
}
data[index] = e;
N++;
}
public void InsertAtFirst(E e)
{
Insert(0, e);
}
public void InsertAtLast(E e)
{
Insert(N, e);
}
#endregion
#region 重写打印、获取元素、修改元素
//重写 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();
}
public E Get(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组索引越界");
}
return data[index];
}
public E GetFirst()
{
return Get(0);
}
public E GetLast()
{
return Get(N - 1);
}
public void Set(int index,E newE)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组索引越界");
}
data[index] = newE;
}
#endregion
#region 包含、查找、删除元素
//判断数组中是否包含某元素
public bool Contains(E e)
{
for (int i = 0; i < N; i++)
{
if (data[i].Equals(e))
{
return true;
}
}
return false;
}
//查找数组元素所在位置
public int GetIndex(E e)
{
for (int i = 0; i < N; i++)
{
if (data[i].Equals(e))
{
return i;
}
}
return -1;
}
//删除某索引处元素
public E RemoveAt(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组索引越界");
}
E del = data[index];
for (int i = index+1; i < N; i++)
{
data[i-1] = data[i];
}
N--;
data[N] = default(E);
if (N ==data.Length/4)
{
RestCapacity(data.Length / 2);
}
return del;
}
public E RemoveAtFirst()
{
return RemoveAt(0);
}
public E RemoveAtLast()
{
return RemoveAt(N-1);
}
//删除某元素
public void Remove(E e)
{
int index = GetIndex(e);
if (index != -1)
{
RemoveAt(index);
}
}
#endregion
#region 扩容操作
private void RestCapacity(int newCapacity)
{
E[] newData = new E[newCapacity];
for (int i = 0; i < N; i++)
{
newData[i] = data[i];
}
data = newData;
}
#endregion
}
}