1.二维数组的使用
使用Rank可以获得数组的维度,使用GetUpperBound(0)可以获得第一维度的最高下表索引值,一次类推
using System;
class Test
{
public static void Main(string[] args)
{
int [,]arr=new int[2,2]{{1,2},{2,3}};
Console.WriteLine(arr.GetUpperBound(0) + 1); //获得数组的行数
Console.WriteLine(arr.GetUpperBound(1) + 1); //获得数组的列数
Console.Read();
}
}
2.动态数组的创建
using System;
class Test
{
public static void Main(string[] args)
{
int row = Convert.ToInt32(Console.ReadLine());
int col = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[row, col];
for (int i = 0; i < arr.GetUpperBound(0) + 1; i++)
for (int j = 0; j < arr.GetUpperBound(1) + 1; j++)
Console.WriteLine(i + j.ToString());
Console.ReadLine();
}
}
3.数组的排序和反转可以使用Array的成员函数Sort和Reverse来实现
using System;
class Test
{
public static void Main(string[] args)
{
int[] arr = new int[] { 2, 9, 6, 5, 4, 3, 1 };
Array.Sort(arr);
Array.Reverse(arr);
foreach (int n in arr)
Console.WriteLine(n);
Console.ReadLine();
}
}
4.ArrayList类
4.1这是一个高级动态数组
using System;
using System.Collections;
class Test
{
public static void Main(string[] args)
{
ArrayList List = new ArrayList(10);
Console.WriteLine(List.Count); //数量树0,容量是10
for (int i = 0; i < List.Capacity; i++)
List.Add(i);
foreach (int n in List)
Console.WriteLine(n);
Console.ReadLine();
}
}
ArrayList判断是否有包含的元素
using System;
using System.Collections;
class Test
{
public static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("TIME");
list.Add("今天你好吗");
Console.WriteLine(list.Contains("TIME"));
}
}
5.hashtable的使用
public vritual void Add(Object key,Object value)这个函数用于向hash表添加元素,由于Hashtable中的元素是键值对,因此需要使用DictionaryEntry类型来遍历,DictionaryEntry类型表示一个键值对的集合
例如
using System;
using System.Collections;
class Test
{
public static void Main(string[] args)
{
Hashtable hash = new Hashtable();
hash.Add("id", "ddddd"); //向hash表添加元素
hash.Add("name", "asdfsa");
hash.Add("sex", "asdcv");
//hash.Add("name", "asdf"); //不能有相同的键
//hash.Clear(); //用于清空hash表
hash.Remove("sex"); //用于移除指定键
Console.WriteLine(hash.Count);
foreach (DictionaryEntry dict in hash) //hashtable的遍历
Console.WriteLine(dict.Key + " " + dict.Value);
Console.WriteLine(hash.Contains("id")); //是否包含特定的键
Console.WriteLine(hash.ContainsValue("id")); //使用包含特定的值
}
}
使用Rank可以获得数组的维度,使用GetUpperBound(0)可以获得第一维度的最高下表索引值,一次类推
using System;
class Test
{
public static void Main(string[] args)
{
int [,]arr=new int[2,2]{{1,2},{2,3}};
Console.WriteLine(arr.GetUpperBound(0) + 1); //获得数组的行数
Console.WriteLine(arr.GetUpperBound(1) + 1); //获得数组的列数
Console.Read();
}
}
2.动态数组的创建
using System;
class Test
{
public static void Main(string[] args)
{
int row = Convert.ToInt32(Console.ReadLine());
int col = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[row, col];
for (int i = 0; i < arr.GetUpperBound(0) + 1; i++)
for (int j = 0; j < arr.GetUpperBound(1) + 1; j++)
Console.WriteLine(i + j.ToString());
Console.ReadLine();
}
}
3.数组的排序和反转可以使用Array的成员函数Sort和Reverse来实现
using System;
class Test
{
public static void Main(string[] args)
{
int[] arr = new int[] { 2, 9, 6, 5, 4, 3, 1 };
Array.Sort(arr);
Array.Reverse(arr);
foreach (int n in arr)
Console.WriteLine(n);
Console.ReadLine();
}
}
4.ArrayList类
4.1这是一个高级动态数组
using System;
using System.Collections;
class Test
{
public static void Main(string[] args)
{
ArrayList List = new ArrayList(10);
Console.WriteLine(List.Count); //数量树0,容量是10
for (int i = 0; i < List.Capacity; i++)
List.Add(i);
foreach (int n in List)
Console.WriteLine(n);
Console.ReadLine();
}
}
ArrayList判断是否有包含的元素
using System;
using System.Collections;
class Test
{
public static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("TIME");
list.Add("今天你好吗");
Console.WriteLine(list.Contains("TIME"));
}
}
5.hashtable的使用
public vritual void Add(Object key,Object value)这个函数用于向hash表添加元素,由于Hashtable中的元素是键值对,因此需要使用DictionaryEntry类型来遍历,DictionaryEntry类型表示一个键值对的集合
例如
using System;
using System.Collections;
class Test
{
public static void Main(string[] args)
{
Hashtable hash = new Hashtable();
hash.Add("id", "ddddd"); //向hash表添加元素
hash.Add("name", "asdfsa");
hash.Add("sex", "asdcv");
//hash.Add("name", "asdf"); //不能有相同的键
//hash.Clear(); //用于清空hash表
hash.Remove("sex"); //用于移除指定键
Console.WriteLine(hash.Count);
foreach (DictionaryEntry dict in hash) //hashtable的遍历
Console.WriteLine(dict.Key + " " + dict.Value);
Console.WriteLine(hash.Contains("id")); //是否包含特定的键
Console.WriteLine(hash.ContainsValue("id")); //使用包含特定的值
}
}