013-列表List的创建和使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _013_列表List的创建和使用
{
class Program
{
static void Main(string[] args)
{
var scoreList = new List<int> {1,2,3};
Console.WriteLine("capacity:" + scoreList.Capacity + "count:" + scoreList.Count);
scoreList.Add(132);
scoreList.Add(45);
Console.WriteLine(scoreList[0]);
Console.WriteLine(scoreList[1]);
Console.ReadKey();
}
}
}
014-列表的遍历
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _014_列表的遍历
{
class Program
{
static void Main(string[] args)
{
var scoreList = new List<int>();
scoreList.Add(34);
scoreList.Add(334);
scoreList.Add(342);
scoreList.Add(343);
scoreList.Add(343);
scoreList.Add(3422);
scoreList.Add(3433);
foreach (int temp in scoreList)
{
Console.Write(temp+" ");
}
Console.ReadKey();
}
}
}
015-操作列表的属性和方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _015_操作列表的属性和方法
{
class Program
{
static void Main(string[] args)
{
var scoreList = new List<int>();
scoreList.Add(100);
scoreList.Add(200);
scoreList.Add(300);
scoreList.Insert(1, -1);
scoreList.RemoveAt(0);
scoreList.Add(200);
int index = scoreList.IndexOf(200);
index = scoreList.IndexOf(400);
int lastIndex = scoreList.LastIndexOf(200);
scoreList.Sort();
Console.ReadKey();
}
}
}
016-泛型-泛型类
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _016_泛型_泛型类
{
class Program
{
static void Main(string[] args)
{
var o1 = new ClassA<int>(12,33);
string str1 = o1.GetSum();
Console.WriteLine(str1);
var o2 = new ClassA<string>("www.","baidu.com");
Console.WriteLine(o2.GetSum());
Console.ReadKey();
}
}
}
ClassA.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _016_泛型_泛型类
{
class ClassA<T>
{
private T a;
private T b;
public ClassA(T a,T b)
{
this.a = a;
this.b = b;
}
public string GetSum()
{
return a+""+b;
}
}
}
017-泛型方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _017_泛型方法
{
class Program
{
public static string GetSum<T>(T a,T b)
{
return a + "" + b;
}
static void Main(string[] args)
{
Console.WriteLine(GetSum<int>(12, 34));
Console.WriteLine(GetSum<string>("你爸爸是","李哥"));
Console.WriteLine(GetSum<double>(12, 34.6));
Console.ReadKey();
}
}
}
018-使用泛型和引索器来实现myList
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _018_使用泛型和索引器来实现一个我们自己的集合类MyList
{
class Program
{
static void Main(string[] args)
{
MyList<int> myList = new MyList<int>();
myList.Add(234);
myList.Add(14);
myList.Add(24);
myList.Add(247);
myList.Insert(0, 1);
myList.RemoveAt(4);
myList.IndexOf(1);
myList.LastIndexOf(1);
myList.Sort();
for(int i =0 ;i<myList.Count;i++)
{
myList[i] = i;
Console.WriteLine(myList[i]);
}
Console.ReadKey();
}
}
}
MyList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _018_使用泛型和索引器来实现一个我们自己的集合类MyList
{
class MyList<T> where T:IComparable
{
private T[] array;
private int count = 0;
public MyList() { array = new T[0]; }
public MyList(int size)
{
if (size >= 0)
{
array = new T[size];
}
}
public int Capacity
{
get { return array.Length; }
}
public int Count
{
get { return count; }
}
public void Add(T item)
{
if (Count == Capacity)
{
if (Capacity == 0)
{
array = new T[4];
}
else
{
var newArray = new T[Capacity*2];
Array.Copy(array,newArray,Count);
array = newArray;
}
}
array[Count] = item;
count++;
}
public T GetItem(int index)
{
if (index >= 0 && index <= count - 1)
{
return array[index];
}
else
{
throw new Exception("索引超出了范围");
}
}
public T this[int index]
{
get {
return GetItem(index);
}
set
{
if (index >= 0 && index <= count - 1)
{
array[index] = value;
}
else
{
throw new Exception("索引超出了范围");
}
}
}
public void Insert(int index, T item)
{
if (index >= 0 && index <= count - 1)
{
if (Count == Capacity)
{
var newArray = new T[Capacity*2];
Array.Copy(array,newArray,Count);
array = newArray;
}
for (int i = count - 1; i >= index; i--)
{
array[i + 1] = array[i];
}
array[index]=item;
count++;
}
else
{
throw new Exception("超出引索范围");
}
}
public void RemoveAt(int index)
{
if (index >= 0 && index <= count - 1)
{
for (int i = index + 1; i < count; i++)
{
array[i - 1] = array[i];
}
count--;
}
else
{
throw new Exception("超出引索范围");
}
}
public int IndexOf(T item)
{
for (int i = 0; i < count; i++)
{
if (array[i].Equals(item))
{
return i;
}
}
return -1;
}
public int LastIndexOf(T item)
{
for (int i = count-1; i >=0; i--)
{
if (array[i].Equals(item))
{
return i;
}
}
return -1;
}
public void Sort()
{
for (int j = 0; j < Count - 1; j++)
{
for (int i = 0; i < Count - 1 - j; i++)
{
if (array[i].CompareTo(array[i + 1]) > 0)
{
T temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
}
}