《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]