C#中的数组是由System.Array
类衍生出来的引用对象,因此可以使用Array类中的各种方法对数组进行各种操作。
一维数组:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 定义一维数组
int[] IntArray = new int[10] {1,2,3,4,5,6,7,8,9,10};
// 定义一维字符串数组
string[] StrArray = new string[3];
StrArray[0] = "abc" ;
StrArray[1] = "abc";
Console.ReadKey();
}
}
}
删除元素(一维数组):
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 定义一维数组
int[] IntArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 遍历数组
foreach (int num in IntArray)
Console.WriteLine(num);
Console.ReadLine();
// 通过循环删除第三个元素
int Del_Num = 2;
for (int x = Del_Num; x < IntArray.Length - Del_Num; x++)
{
IntArray[x] = IntArray[x - 1];
}
Console.ReadKey();
}
}
}
寻找最大最小值:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 定义一维数组
int[] Array = new int[10] { 57, 32, 78, 96, 33, 11, 78, 3, 78, 2 };
// 声明两个变量用来存储最大值和最小值
int min = int.MaxValue;
int max = int.MinValue;
int sum = 0;
for (int i = 0; i < Array.Length; i++)
{
if (Array[i] > max)
max = Array[i];
if (Array[i] < min)
min = Array[i];
sum += Array[i];
}
Console.WriteLine("最大值: {0} 最小值: {1} 总和: {2} 平均值: {3}", max, min, sum, sum / Array.Length);
Console.ReadKey();
}
}
}
数组组合为字符串:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] name = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" };
string str = null;
for (int x = 0; x < name.Length - 1; x++)
str += name[x] + "|";
Console.WriteLine(str + name[name.Length - 1]);
Console.ReadKey();
}
}
}
数组元素反转:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] name = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" };
string tmp;
for (int x = 0; x < name.Length / 2; x++)
{
tmp = name[name.Length - 1 - x];
name[x] = name[name.Length - 1 - x];
name[name.Length - 1 - x] = tmp;
}
for (int x = 0; x < name.Length - 1; x++)
Console.Write(name[x] + " |" );
Console.ReadKey();
}
}
}
冒泡排序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 执行排序
static void Sort(int[] Array)
{
for (int x = 0; x < Array.Length - 1; x++)
{
for (int y = 0; y < Array.Length - 1 - x; y++)
{
if (Array[y] > Array[y + 1])
{
int tmp = Array[y];
Array[y] = Array[y + 1];
Array[y+1] = tmp;
}
}
}
}
// 输出结果
static void Display(int[] Array)
{
for (int x = 0; x < Array.Length; x++)
{
Console.Write(Array[x] + " ");
}
}
static void Main(string[] args)
{
int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 };
Sort(MyArray);
Display(MyArray);
// 使用系统提供的方法排序
Array.Sort(MyArray);
// 执行一次反向排序
Array.Reverse(MyArray);
Console.ReadKey();
}
}
}
直接插入排序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 执行排序
static void Sort(int[] Array)
{
for (int x = 0; x < Array.Length; x++)
{
int tmp = Array[x];
int y = x;
while ((y > 0) && (Array[y - 1] > tmp))
{
Array[y] = Array[y-1];
--y;
}
Array[y] = tmp;
}
}
static void Main(string[] args)
{
int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 };
Sort(MyArray);
foreach (int x in MyArray)
Console.Write(x + " ");
Console.ReadKey();
}
}
}
选择排序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 执行排序
static void Sort(int[] Array)
{
int min = 0;
for (int x = 0; x < Array.Length; x++)
{
min = x;
for (int y = x + 1; y < Array.Length; y++)
{
if (Array[y] < Array[min])
min = y;
}
int tmp = Array[min];
Array[min] = Array[x];
Array[x] = tmp;
}
}
static void Main(string[] args)
{
int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 };
Sort(MyArray);
foreach (int x in MyArray)
Console.Write(x + " ");
Console.ReadKey();
}
}
}
定义二维数组
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 定义二维数组
int[,] Array = new int[2,3]{{1,2,4},{4,5,6}};
Console.WriteLine("数组行数为: {0}", Array.Rank);
Console.WriteLine("数组列数为: {0}", Array.GetUpperBound(Array.Rank - 1) + 1);
for (int x = 0; x < Array.Rank;x++ )
{
string str = "";
for(int y=0;y< Array.GetUpperBound(Array.Rank-1)+1;y++)
{
str = str + Convert.ToString(Array[x, y]) + " ";
}
Console.Write(str);
}
Console.ReadKey();
}
}
}
定义动态二维数组:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int Row = Convert.ToInt32(Console.ReadLine());
int Col = Convert.ToInt32(Console.ReadLine());
int[,] Array = new int[Row, Col];
for (int x = 0; x < Row; x++)
{
for (int y = 0; y < Col; y++)
{
Console.Write(x + "-->" + y.ToString() + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
一维数组的合并:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] Array1 = new int[] { 1, 2, 3, 4, 5 };
int[] Array2 = new int[] { 6, 7, 8, 9, 10 };
// 将Array1 与 Array2 合并成 Array3
int Count = Array1.Length + Array2.Length;
int[] Array3 = new int[Count];
for (int x = 0; x < Array3.Length; x++)
{
if (x < Array1.Length)
Array3[x] = Array1[x];
else
Array3[x] = Array2[x - Array1.Length];
}
foreach (int each in Array3)
Console.Write(each + " ");
Console.ReadKey();
}
}
}
二维数组的合并:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] Array1 = new int[] { 1, 2, 3, 4, 5 };
int[] Array2 = new int[] { 6, 7, 8, 9, 10 };
// 将两个一维数组,合并到一个二维数组中
int[,] Array3 = new int[2, 5];
// Rank = 二维数组中的2
for (int x = 0; x < Array3.Rank; x++)
{
switch (x)
{
case 0:
{
for (int y = 0; y < Array1.Length; y++)
Array3[x, y] = Array1[y];
break;
}
case 1:
{
for (int z = 0; z < Array2.Length; z++)
Array3[x, z] = Array2[z];
break;
}
}
}
// 输出二维数组中的数据
for (int x = 0; x < Array3.Rank;x++ )
{
for(int y=0;y<Array3.GetUpperBound(Array3.Rank-1)+1;y++)
Console.Write(Array3[x, y] + " ");
Console.WriteLine();
}
Console.ReadKey();
}
}
}
二维数组的拆分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] Array = new int[2, 3] { { 1, 3, 5 }, { 3, 4, 6 } };
int[] ArrayOne = new int[3];
int[] ArrayTwo = new int[4];
for (int x = 0; x < 2; x++)
{
for(int y= 0; y<3; y++)
{
switch(x)
{
case 0: ArrayOne[y] = Array[x, y]; break;
case 1: ArrayTwo[y] = Array[x, y]; break;
}
}
}
foreach (int each in ArrayOne)
Console.WriteLine(each);
Console.ReadKey();
}
}
}
ArrayList 类位于System.Collections
命名空间下,它可以动态添加和删除元素,可以将该数组类看作扩充了功能的数组。
动态数组创建:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 动态创建 ArrayList 并初始化10个数据
ArrayList List = new ArrayList(10);
for (int x = 0; x < 9; x++)
List.Add(x);
Console.WriteLine("可包含元素数量: {0} ", List.Capacity);
Console.WriteLine("实际包含数量: {0}", List.Count);
foreach (int each in List)
Console.Write(each + " ");
Console.WriteLine();
// 将普通数组添加到ArrayList中
int[] Array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ArrayList List1 = new ArrayList(Array);
for (int x = 0; x < List1.Count; x++)
Console.Write(List1[x] + " ");
Console.ReadKey();
}
}
}
增加/插入/删除元素:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Display(ArrayList x)
{
foreach (int each in x)
Console.Write(each + " ");
Console.WriteLine();
}
static void Main(string[] args)
{
// 动态创建 ArrayList
ArrayList List = new ArrayList(10);
// 像数组增加数据
List.Add(100);
List.Add(200);
List.Add(300);
List.Add(400);
List.Add(500);
Display(List);
// 插入数据
List.Insert(1, 1000);
List.Insert(2, 2000);
Display(List);
// 移除指定元素
List.Remove(1000);
Display(List);
// 根据索引移除元素
List.RemoveAt(1);
Display(List);
// 判断集合中是否包含指定元素
bool ret = List.Contains(100);
Console.WriteLine(ret);
// 移除一个范围,从下标1开始向后移除3个元素
List.RemoveRange(1, 3);
Display(List);
// 清空所有集合
List.Clear();
Console.ReadKey();
}
}
}
生成随机数存入集合:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
// 创建集合,添加数字,求平均值与和,最大值,最小值
list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
int sum = 0;
int max = (int)list[0];
for (int x = 0; x < list.Count;x++ )
{
if((int)list[x] > max)
max = (int)list[x];
sum += (int)list[x];
}
Console.WriteLine("最大值: {0} 总和: {1} 平均值: {2}",max,sum,sum/list.Count);
list.Clear();
// 用来生成随机数,并去重后放入list链表中
Random rand = new Random();
for (int x = 0; x < 10;x++ )
{
int num = rand.Next(0,10);
// 判断集合中是否有这个随机数
if (!list.Contains(num))
list.Add(num);
else
x--;
}
foreach (int each in list)
Console.WriteLine(each);
Console.ReadKey();
}
}
}
增加并遍历数组: 我们可以直接将多个数组放入到ArrayList容器中,进行存储。
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
// 直接追加匿名数组
list.Add(new int[] { 1, 2, 3, 4, 5 });
list.Add(new int[] { 6, 7, 8, 9, 10 });
// 定义并追加数组
int[] ptr = new int[5] { 100, 200, 300, 400, 500 };
list.Add(ptr);
for (int x = 0; x < list.Count;x++ )
{
if (list[x] is int[])
{
for(int y=0; y < ((int[])list[x]).Length; y++)
{
Console.Write(((int[])list[x])[y] + " ");
}
Console.WriteLine();
}
}
Console.ReadKey();
}
}
}
增加遍历结构体:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public struct Student
{
public int u_id;
public string u_name;
public int u_age;
public Student(int id, string name, int age)
{
this.u_id = id;
this.u_name = name;
this.u_age = age;
}
}
static void Main(string[] args)
{
ArrayList list = new ArrayList();
// 定义三个结构
Student stu1 = new Student(1001,"admin",22);
Student stu2 = new Student(1002, "guest", 33);
Student stu3 = new Student(1003, "lyshark", 19);
// 将结构追加到链表
list.Add(stu1);
list.Add(stu2);
list.Add(stu3);
// 遍历结构体
for (int x = 0; x < list.Count;x++ )
{
if (list[x] is Student)
{
Student ptr = (Student)list[x];
Console.WriteLine("ID: {0} 姓名: {1} 年龄: {2}", ptr.u_id, ptr.u_name, ptr.u_age);
}
}
Console.ReadKey();
}
}
}
队列的使用:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
// 入队
for (int x = 0; x < 10;x++ )
{
queue.Enqueue(x);
Console.WriteLine("{0} 入队 -> 队列计数: {1}", x,queue.Count);
}
// 遍历队列
foreach(int each in queue)
{
Console.WriteLine("队列开始: {0} --> 队列元素: {1}", queue.Peek().ToString(),each);
}
// 弹出队列
while(queue.Count !=0)
{
int value = (int)queue.Dequeue();
Console.WriteLine("{0} 出队列.", value);
}
Console.ReadKey();
}
}
}
栈操作:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
// 向栈追加数据
for (int x = 0; x < 10; x++)
stack.Push(x);
// 查询栈
Console.WriteLine("当前栈顶元素为:{0}", stack.Peek().ToString());
Console.WriteLine("移出栈顶元素:{0}", stack.Pop().ToString());
Console.WriteLine("当前栈顶元素为:{0}", stack.Peek().ToString());
// 遍历栈
foreach (int each in stack)
Console.WriteLine(each);
// 出栈
while(stack.Count !=0)
{
int pop = (int)stack.Pop();
Console.WriteLine("{0} 出栈", pop);
}
Console.ReadKey();
}
}
}
hash表的使用 Hashtable 哈希表,他表示键值对的一个集合,这些键值对根据键的哈希代码进行组织,键不可以为空,值可以为空。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Hashtable hash = new Hashtable();
// 添加键值对 key = value
hash.Add("id", 1001);
hash.Add("name", "lyshark");
hash.Add("sex", "男");
Console.WriteLine("hash 元素个数: {0}", hash.Count);
// 移除一个hash值
hash.Remove("sex");
// 根据hash查找 是否存在
Console.WriteLine("根据key查找: {0}", hash.Contains("name"));
Console.WriteLine("根据key查找: {0}", hash.ContainsValue("lyshark"));
// 遍历hash表
foreach (DictionaryEntry each in hash)
Console.WriteLine(each.Key + " " + each.Value);
Console.WriteLine();
// 清空hash表
hash.Clear();
Console.ReadKey();
}
}
}
有序哈希表 SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问。
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SortedList student = new SortedList();
// 向序列追加集合
student.Add("1001", "Lucy");
student.Add("1002", "Lily");
student.Add("1003", "Tom");
// 先判断是否存在某个值然后咋追加
if (!student.ContainsValue("LyShark"))
student.Add("1004", "LyShark");
// 遍历学生数据
foreach(DictionaryEntry each in student)
{
string id = each.Key.ToString();
string name = each.Value.ToString();
Console.WriteLine("ID: {0} 姓名: {1}", id, name);
}
// 删除一个数据
student.Remove("1001");
// 获取键的集合
ICollection key = student.Keys;
foreach(string each in key)
{
Console.WriteLine(each + "--> " + student[each]);
}
Console.ReadKey();
}
}
}
泛型类型集合: 效率更高更快,不发生装箱,拆箱等。
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 创建泛型集合对象
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });
list.AddRange(list);
// List泛型集合可以转换为数组
int[] array = list.ToArray();
Console.WriteLine("数组成员数: {0}", array.Length);
// 字符数组转换为泛型集合
char[] chs = new char[] { 'a', 'b', 'c' };
List<char> list_char = chs.ToList();
Console.WriteLine("字符数组成员数: {0}",list_char.Count);
Console.ReadKey();
}
}
}
k-v泛型集合: 使用队组,实现的泛型集合。
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "张三");
dict.Add(2, "李四");
dict.Add(3, "王五");
foreach(KeyValuePair<int,string> each in dict)
{
Console.WriteLine("序号:{0} 数值:{1}", each.Key, each.Value);
}
foreach(var each in dict.Keys)
{
Console.WriteLine("序号:{0} 数值:{1}", each, dict[each]);
}
Console.ReadKey();
}
}
}
k-v泛型集合: 统计指定的一个字符串中单词的出现频率。
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String str = "welcome to china";
// 对组统计出每个字符串出现的次数
Dictionary<char, int> dict = new Dictionary<char, int>();
for (int x = 0; x < str.Length;x++ )
{
if (str[x] == ' ')
continue;
//如果dic已经包含了当前循环到的这个键
if (dict.ContainsKey(str[x]))
dict[str[x]]++;
// 这个字符在集合当中是第一次出现
else
dict[str[x]] = 1;
}
// 遍历出数量
foreach(KeyValuePair<char,int> each in dict)
{
Console.WriteLine("字母: {0} 出现了: {1} 次", each.Key, each.Value);
}
Console.ReadKey();
}
}
}