ArrayList和HashTable集合

1.ArrayList集合

***添加元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//
ArrayList list = new ArrayList(); //集合:很多数据的一个集合,长度可以任意改变,类型随便
//数组:长度不可变,类型单一 list.Add();
list.Add(true);
list.Add("zq");
list.Add('w'); /*
* ToString方法
我们将一个对象输出到控制台 默认情况下 打印的就是这个对象所在的类的命名空间
*/
list.Add(new int []{,,,,,,});
Person p = new Person();
list.Add(p);
list.Add(list); for(int i=;i<list.Count;i++)
{
if(list[i] is Person)
{
((Person)list[i]).Say();
}
else if(list[i] is int[])
{
int[] array = (int[])list[i];
for(int j=;j<array.Length;j++)
{
Console.WriteLine(array[j]);
}
}
else
{
Console.WriteLine(list[i]);
} //Console.WriteLine(list[i]);
} Console.ReadKey();
}
} public class Person
{
public void Say()
{
Console.WriteLine("say!");
}
}
}

***添加集合元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO; namespace ArrayList
{
class Program
{
static void Main(string[] args)
{ System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add(); //添加集合元素
list.AddRange(new int[] { , , , , , , , , });
list.AddRange(list);
for(int i=;i<list.Count;i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey(); }
}
}

***集合的操作(插入,删除,清空,反转,排序)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO; namespace ArrayList
{
class Program
{
static void Main(string[] args)
{ System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add();
list.Add(true);
//添加集合元素
list.AddRange(new int[] { , , , , , , , , });
list.AddRange(list); //*******************************
//list.Clear();//清空所以元素
//list.Remove(true);//删除单个元素
//list.RemoveAt(0);//根据下标去删除元素
//list.RemoveRange(0, 3);//根据下标去删除一定范围内的元素
//list.Reverse();//反转
//list.Sort();//升序排列
list.Insert(, "insert");//在指定的位置,插入一个元素
list.InsertRange(, list);//在指定的位置,插入一个集合
bool tt= list.Contains();//判断集合包含不包含改元素 for(int i=;i<list.Count;i++)
{
Console.WriteLine(list[i]);
} Console.ReadKey(); }
}
}

***集合的长度

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ArrayList长度问题
{
class Program
{
static void Main(string[] args)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add();
//Count:表示这个集合中实际包含的元素个数
//Capacity:表示这个集合中可以包含的元素个数
Console.WriteLine(list.Count);
Console.WriteLine(list.Capacity); //****
//每次集合中实际包含的个数(count)超过了可以包含的元素个数(Capacity)的时候,
//集合就会在内存中申请多开辟一倍的空间,来保证集合的长度够用。 Console.ReadKey(); }
}
}

2.Hashtable 集合 --> 键值对集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Diagnostics; namespace HashTable
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(,"");
ht.Add(, "");
ht.Add(true, "True");
ht.Add(false, "False"); ht[] = ""; //在键值对集合中 是根据键去找值得
//Console.WriteLine(ht[true]);
foreach (var item in ht.Keys)
{
Console.WriteLine(ht[item]);
}
//for (int i = 0; i < ht.Count;i++ )
//{
// Console.WriteLine(ht[i]);
//} //键值对里面的键是唯一的,值可以重复! //常用方法:判断键是否唯一
if(!ht.ContainsKey())
{
ht.Add(, "");
}
else
{
Console.WriteLine("已包含");
} //常用方法
ht.Clear();
ht.Remove(); //****
//问:for/foreach 在循环次数很多很多的情况下,谁的效率高
//foreach循环效率要高很多! //****测试程序运行时间
/*
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000000; i++)
{ }
sw.Stop();
Console.WriteLine(sw.Elapsed);
* */ Console.ReadKey();
}
}
}

**C# var 关键字

地址:https://www.cnblogs.com/ggll611928/p/5991401.html

3.简体繁体子转换 用Hasgtable集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace ConsoleApplication2
{
class Program
{
private const string jian = "常数声明可以声明多个常数";
private const string fan = "常2聲明可以聲明多個常數"; static void Main(string[] args)
{
Hashtable ht = new Hashtable(); //Console.WriteLine(jian[1]);
for (int i = ; i < jian.Length; i++)
{
if(!ht.ContainsKey(jian[i]))
{
ht.Add(jian[i], fan[i]);
}
} string input = Console.ReadLine(); for (int i = ; i < input.Length; i++)
{
if(ht.ContainsKey(input[i]))
{
Console.Write(ht[input[i]]);
}
else
{
Console.Write(input[i]);
} }
Console.ReadKey();
}
}
}

C#面向对象12 集合的更多相关文章

  1. 面向对象之集合ArrayList

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  2. Java阶段性测试--知识点:数组,面向对象,集合、线程,IO流

    #Java基础测试 涉及知识点:数组,面向对象,重载,重写,继承,集合,排序,线程,文件流 一.多项选择题(可能是单选,也可能是多选) 1.下列标识符命名不合法的是(   D  ). A.$_Name ...

  3. Java学习日记-12 集合(2)

    一.List<E>接口(超级接口Collection,List比Collection多重载了一些索引作为形参的方法)1.实现类ArrayList\LinkedListArrayList顺序 ...

  4. 12集合(3)-----Map

    一.总体分类 Collection(包括方法add,remove,contains,clear,size) List(接口) LinkedList ArrayList Vector---Stack 2 ...

  5. 12集合(2)-----Set

    一.总体分类 Collection(包括方法add,remove,contains,clear,size) List(接口) LinkedList ArrayList Vector---Stack 2 ...

  6. 12集合(1)-----List

    一.总体分类 Collection(包括方法add,remove,contains,clear,size) List(接口) LinkedList ArrayList Vector---Stack 2 ...

  7. note 12 集合Set

    集合Set +无序不重复元素(键)集 +和字典类似,但是无"值" 创建 x = set() x = {key1,key2,...} 添加和删除 x.add('body') x.re ...

  8. plsql programming 12 集合(忽略, 个人感觉用不到)

    关联数组, 嵌套表, varray 个人并不推荐使用集合, 因为操作有别于普通字段. 集合中每一个元素的数据类型都是相同的, 因此这些元素都是同质的(同质元素) 这一章的内容先忽略吧, 因为个人感觉用 ...

  9. Java面向对象12——static详解

    static  package oop.demon01.demon07; ​ // static : public class Student { ​     private static int a ...

随机推荐

  1. Netty使用(一)

    1.Netty介绍 Netty是一款基于NIO(Nonblocking I/O,非阻塞IO)开发的网络通信框架: 提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器 ...

  2. windows服务器安装nodejs实现自动计划

    直接官网打开:https://nodejs.org/en/ 下载相应的系统nodejs版本直接安装后,通过命令行window+r    输入node + web(目录)+\pubils\app.js  ...

  3. 查找与排序算法(Searching adn Sorting)

    1,查找算法 常用的查找算法包括顺序查找,二分查找和哈希查找. 1.1 顺序查找(Sequential search) 顺序查找: 依次遍历列表中每一个元素,查看是否为目标元素.python实现代码如 ...

  4. Nginx作为静态资源web服务-跨站访问

    一.跨域访问 1.什么是跨域? 参看我的另一篇博客(https://www.cnblogs.com/chrdai/p/11280895.html) 2.为什么浏览器禁止跨域访问? 不安全,容易出现CS ...

  5. usage memcache in linux

    set和add的区别 set可以重写存在的键值对, 也可以添加新的/ 而add不行, 如果存在已有的键名, 则add不会做更新该键值对, 不做任何事, 就是一次无效操作, 也就是, add可以防止重写 ...

  6. [go]redis基本使用

    redis 数据结构操作 import ( "github.com/garyburd/redigo/redis" ) // set c, err := redis.Dial(&qu ...

  7. 加载大图片的OOM异常

    * 原因:系统分配给应用程序的堆内存 < 图片的大小* 解决方案:缩放图片显示* OOM:OutOfMemoryError * 图片的宽高 * 宽 2400 * 高 3200 * 手机屏幕的宽高 ...

  8. ObjectId初探

    ObjectId MongoDB每个集合存储的每个文档必须有一个"_id"键,默认是个ObjectId对象. "_id"作为当前文档在集合的唯一标识. 71st ...

  9. 记一次ceph集群的严重故障 (转)

    问题:集群状态,坏了一个盘,pg状态好像有点问题[root@ceph-1 ~]# ceph -s    cluster 72f44b06-b8d3-44cc-bb8b-2048f5b4acfe     ...

  10. 用Red5搭建支持WEB播放的实时监控视频

    用Red5搭建支持WEB播放的实时监控视频 1. 下载Red5:https://github.com/Red5/red5-server/releases 下载了Red5 1.0.6 release的Z ...