c#数组使用复习
来源:互联网 发布:php数组打印 编辑:程序博客网 时间:2024/06/03 22:55
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")); //使用包含特定的值
}
}
0 0
- c#数组使用复习
- C#复习之数组
- C复习笔记(数组的使用)
- c#中正则表达式使用实例复习
- 数组复习
- 复习数组
- 数组 复习
- C#中的数组使用
- C#数组使用
- C#使用多维数组
- C# 数组的使用
- C#使用参数数组
- C#数组的使用
- c#复习
- C#数组-使用数组的索引
- C#中使用控件数组
- c# 多维数组的使用
- C# 数组 使用速查
- python3.5爬虫完成笔趣阁小说的爬取
- Bellman-Ford算法,解决负权回路。
- app推广螺旋管理
- C++ POD 类型
- Android多线程
- c#数组使用复习
- 阅读器(慕客学习)之JS设计模式分解
- 【web前端开发技术】深入理解JavaScript异步编程--JavaScript异步编程的终极演变
- sendRedirect和forward区别
- java初级之25对象类型的参数传递
- servlet的数据库操作风格
- Android 图片Drawable类型之Level List
- AsyncTask的使用
- c#结构体-多态复习(1)