静态数组:int[]、float[]、double[] 、char[]、string[]
特点:数组一旦创建,其容量的大小是无法改变的
int[] arr = new int[20]
虽然我这边里可以提前设定好了容量,但是存储数据的一种,我们永远都想象不到会有多少数据会被进行存储,所以适当的使用动态数组,是最好的!
扩展知识(1):
首先,图片中的 this 代表正在运行的当前实例,也就是这个类的一个构造器! 最后,我们可以知道 public Array1() : this(10) Array1():this(10) 表示的是调用自己类中的另一个构造器(Array1(int capacity))
扩展知识(2):
stringBuilder** 用于字符串连接,并且效率远高于string 用“+”将整数类型进行拼接的效率要快。 StringBuilder.Append:将信息追加到当前 StringBuilder 的结尾! string.Format **
对数组进行增加、添加、修改、查询、删除的例子:
Add 函数中的 For循环是让数组的 元素通通往后移** ,然后(date[index] = e;N++;)是在数组中添加元素。 而之所以要重写 Tostring(),首先在不重写的情况下,我们输出 a ,调用的是 base.object.Tostring(),只会输出 命名空间.类名 所以重写 Tostring(),让他变为我们想要输出的结果。 代码逻辑就是:在 For 循环中添加、修改元素,然后重写 Tostring()**进行返回输出。
class Array1
{
private int[] date;
private int N;
/// <summary>
/// 这个构造器,让用户自己填写容量
/// </summary>
/// <param name="capacity">用户添加的数组容量</param>
public Array1(int capacity)
{
date = new int[capacity];
N = 0;
}
public Array1() : this(10) { }
public int Capacity
{
get
{
return date.Length;
}
}
public int Count
{
get
{
return N;
}
}
public bool IsEmpty
{
get
{
return N == 0;
}
}
public void Add(int index, int e)
{
if (index < 0 || index > N)
{
throw new ArgumentNullException("数组索引越界");
}
if (N == date.Length)
{
throw new ArgumentNullException("数组已满");
}
for (int i = N - 1; i >= index; i--)
date[i + 1] = date[i];
date[index] = e;
N++;
}
public void AddLast(int e)
{
Add(N, e);
}
public void AddFirst(int e)
{
Add(0, e);
}
/// <summary>
/// 通过索引,来获取数组的元素
/// </summary>
/// <param name="index">数组的长度-1</param>
/// <returns></returns>
public int Get(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
return date[index];
}
/// <summary>
/// 获取数组的第一个元素
/// </summary>
/// <returns></returns>
public int GetFirst()
{
return Get(0);
}
/// <summary>
/// 获取数组的最后一个元素
/// </summary>
/// <returns></returns>
public int GetLast()
{
return Get(N - 1);
}
/// <summary>
/// 更改指定的数组元素
/// </summary>
/// <param name="index">索引</param>
/// <param name="newE">修改的数字</param>
/// <returns></returns>
public int Set(int index, int newE)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
return date[index] = newE;
}
/// <summary>
/// 查看是否有传进来的值
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public bool GetNumber(int e)
{
for (int i = 0; i < N; i++)
{
if (date[i] == e)
{
return true;
}
}
return false;
}
/// <summary>
/// 获取到传进来的值,当前的索引
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public int GetIndex(int e)
{
for (int i = 0; i < N; i++)
{
if (date[i] == e)
{
return i;
}
}
return -1;
}
/// <summary>
/// 删除指定的数组元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int RemoveAt(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
int del = date[index];
for (int i = index; i < N; i++)
date[i] = date[i + 1];
N--;
date[N] = default(int);
return del;
}
public int RemoveFirst()
{
return RemoveAt(0);
}
public int RemoveLast()
{
return RemoveAt(N - 1);
}
/// <summary>
/// 删除指定元素的索引
/// </summary>
/// <param name="e"></param>
public void Remove(int e)
{
int index = date[e];
if (index != -1)
{
RemoveAt(index);
}
}
public override string ToString()
{
StringBuilder res = new StringBuilder();
res.Append(string.Format("Array1:count = {0} capacity = {1}\n", N, date.Length));
res.Append("[");
for (int i = 0; i < N; i++)
{
res.Append(date[i]);
if (i != N - 1)
res.Append(",");
}
res.Append("]");
return res.ToString();
}
}
class Program
{
private static void Main(string[] args)
{
Array1 a = new Array1(20);
for (int i = 0; i < 10; i++)
{
a.AddLast(i);
}
Console.WriteLine(a);
a.AddFirst(66);
Console.WriteLine(a);
a.Add(2, 77);
Console.WriteLine(a);
Console.WriteLine("==========分隔符===========");
Console.WriteLine(a.Get(8));
Console.WriteLine(a.GetFirst());
Console.WriteLine(a.GetLast());
a.Set(1, 200);
Console.WriteLine(a);
Console.WriteLine("==========分隔符===========");
Console.WriteLine(a.GetNumber(5));
Console.WriteLine(a.RemoveAt(5));
Console.WriteLine(a.GetIndex(9));
Console.WriteLine(a);
Console.WriteLine("==========分隔符===========");
a.RemoveAt(2);
a.RemoveFirst();
a.RemoveLast();
a.Remove(4);
Console.WriteLine(a);
Console.ReadKey();
}
}
运行结果:
动态数组:
特点:可以根据元素的多少动态地调整数组容量的大小,就算先前设置好容量,我们再次进行添加数据,因为动态数组具有自动扩容的能力
缺点:我们总是未能充分使用到动态数组的空间。
动态数组的例子:
通过计算当前数组的元素是否为原来的四分之一,进行缩容 是否为超出原来的数组容量,进行扩容
class Array1{
public void Add(int index, int e)
{
if (index < 0 || index > N)
{
throw new ArgumentNullException("数组索引越界");
}
if (N == date.Length)
{
ResetCapacity(2 * date.Length);
}
for (int i = N - 1; i >= index; i--)
date[i + 1] = date[i];
date[index] = e;
N++;
}
/// <summary>
/// 删除指定的数组元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int RemoveAt(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
int del = date[index];
for (int i = index; i < N; i++)
date[i] = date[i + 1];
N--;
date[N] = default(int);
if (N == date.Length / 4)
{
ResetCapacity(date.Length / 2);
}
return del;
}
}
class Program
{
private static void Main(string[] args)
{
Array1 a = new Array1(10);
for (int i = 0; i < 10; i++)
{
a.AddLast(i);
}
Console.WriteLine(a);
a.AddLast(100);
Console.WriteLine(a) ;
for (int i = 0; i < 6; i++)
{
a.RemoveLast();
Console.WriteLine(a);
}
Console.ReadKey();
}
}
运行结果:
ArrayList、List(泛型)
泛型和非泛型的例子:
泛型和非泛型 泛型数组List有两个优势: 第一个是对于存储值类型数据,性能更优 第二个是使代码更清晰和保证类型安全 List
l = newList (); ArrayList a = nwe ArrayList();
class Program
{
private static void Main(string[] args)
{
int n = 10000000;
Stopwatch t1 = new Stopwatch();
Stopwatch t2 = new Stopwatch();
Stopwatch t3 = new Stopwatch();
Stopwatch t4 = new Stopwatch();
Console.WriteLine("测试值类型对象int");
t1.Start();
List<int> l = new List<int>();
for (int i = 0; i < n; i++)
{
l.Add(i); //不发生装箱
int x = l[i]; //不发生拆箱
}
t1.Stop();
Console.WriteLine("List'Time:" + t1.ElapsedMilliseconds+"ms");
t2.Start();
ArrayList a = new ArrayList();
for (int i = 0; i < n; i++)
{
a.Add(i); //发生装箱
int x = (int)a[i]; //发生拆箱
}
t2.Stop();
Console.WriteLine("List'Time:" + t2.ElapsedMilliseconds + "ms");
Console.WriteLine("测试引用类型对象string");
t3.Start();
List<string> l2 = new List<string>();
for (int i = 0; i < n; i++)
{
l2.Add("X"); //不发生装箱
string x = l2[i]; //不发生拆箱
}
t3.Stop();
Console.WriteLine("List'Time:" + t3.ElapsedMilliseconds + "ms");
t4.Start();
ArrayList a2 = new ArrayList();
for (int i = 0; i < n; i++)
{
a2.Add("x"); //不发生装箱
string x = (string)a2[i]; //不发生拆箱
}
t4.Stop();
Console.WriteLine("List'Time:" + t4.ElapsedMilliseconds + "ms");
Console.ReadKey();
}
}
运行结果:
运用泛型对动态数组添加元素的例子:
class Array1<T>
{
private T[] date;
private int N;
/// <summary>
/// 这个构造器,让用户自己填写容量
/// </summary>
/// <param name="capacity">用户添加的数组容量</param>
public Array1(int capacity)
{
date = new T[capacity];
N = 0;
}
public Array1() : this(10) { }
public int Capacity
{
get
{
return date.Length;
}
}
public int Count
{
get
{
return N;
}
}
public bool IsEmpty
{
get
{
return N == 0;
}
}
public void Add(int index, T e)
{
if (index < 0 || index > N)
{
throw new ArgumentNullException("数组索引越界");
}
if (N == date.Length)
{
ResetCapacity(2 * date.Length);
}
for (int i = N - 1; i >= index; i--)
date[i + 1] = date[i];
date[index] = e;
N++;
}
public void AddLast(T e)
{
Add(N, e);
}
public void AddFirst(T e)
{
Add(0, e);
}
/// <summary>
/// 通过索引,来获取数组的元素
/// </summary>
/// <param name="index">数组的长度-1</param>
/// <returns></returns>
public T Get(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
return date[index];
}
/// <summary>
/// 获取数组的第一个元素
/// </summary>
/// <returns></returns>
public T GetFirst()
{
return Get(0);
}
/// <summary>
/// 获取数组的最后一个元素
/// </summary>
/// <returns></returns>
public T GetLast()
{
return Get(N - 1);
}
/// <summary>
/// 更改指定的数组元素
/// </summary>
/// <param name="index">索引</param>
/// <param name="newE">修改的数字</param>
/// <returns></returns>
public void Set(int index, T newE)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
date[index] = newE;
}
/// <summary>
/// 查看是否有传进来的值
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public bool GetNumber(int e)
{
for (int i = 0; i < N; i++)
{
if (date[i].Equals(e))
{
return true;
}
}
return false;
}
/// <summary>
/// 获取到传进来的值,当前的索引
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public int GetIndex(int e)
{
for (int i = 0; i < N; i++)
{
if (date[i].Equals(e))
{
return i;
}
}
return -1;
}
/// <summary>
/// 删除指定的数组元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T RemoveAt(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组超出索引");
}
T del = date[index];
for (int i = index; i < N; i++)
date[i] = date[i + 1];
N--;
date[N] = default(T);
if (N == date.Length / 4)
{
ResetCapacity(date.Length / 2);
}
return del;
}
public T RemoveFirst()
{
return RemoveAt(0);
}
public T RemoveLast()
{
return RemoveAt(N - 1);
}
/// <summary>
/// 删除指定元素的索引
/// </summary>
/// <param name="e"></param>
public void Remove(int e)
{
int index = e;
if (index != -1)
{
RemoveAt(index);
}
}
private void ResetCapacity(int newCapcaity)
{
T[] newDate = new T[newCapcaity];
for (int i = 0; i < N; i++)
newDate[i] = date[i];
date = newDate;
}
public override string ToString()
{
StringBuilder res = new StringBuilder();
res.Append(string.Format("Array1:count = {0} capacity = {1}\n", N, date.Length));
res.Append("[");
for (int i = 0; i < N; i++)
{
res.Append(date[i]);
if (i != N - 1)
res.Append(",");
}
res.Append("]");
return res.ToString();
}
}
class Program
{
private static void Main(string[] args)
{
int[] n = { 1, 2, 3, 4, 5, 6, 7 };
Array1<int> a = new Array1<int>();
for (int i = 0; i < n.Length; i++)
a.AddLast(n[i]);
Console.WriteLine(a);
string[] s = { "1","2","3","4","5" };
Array1<string> a1 = new Array1<string>();
for (int i = 0; i < s.Length; i++)
a1.AddLast(s[i]);
Console.WriteLine(a1);
Console.ReadKey();
}
}