C#面向对象14 List泛型集合/装箱和拆箱/字典集合(Dictionary)
1.List泛型集合
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace List泛型集合
{
class Program
{
static void Main(string[] args)
{
//
List<int> list = new List<int>();
list.Add();
list.Add();
list.AddRange(new int[] { , , , , , , , , });
list.AddRange(list); //List泛型集合可以转换成数组
int [] nums = list.ToArray();
Console.WriteLine(nums.Length);
for (int i = ; i < nums.Length; i++)
{
Console.WriteLine(nums[i]); }
/*
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}*/
//数组转LIST泛型集合
char[] chars = new char [] { 'a','b','c','d','e'};
List<char> listchars = chars.ToList();
for (int i = ; i < listchars.Count; i++)
{
Console.WriteLine(listchars[i]);
} Console.ReadKey();
}
}
}
2.装箱拆箱
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 装箱和拆箱
{
class Program
{
static void Main(string[] args)
{
//******
//装箱:将值类型转换为引用类型
//拆箱:将引用类型转换为值类型
//条件:看两种类型是否发生了装箱和拆箱,要看,这两种类型是否存在继承关系。 int n = ;
object o = n;//装箱
int nn = (int)o;//拆箱 //装箱操作
ArrayList list = new ArrayList();
for (int i = ; i < ; i++)
{
list.Add(i);
} //没有发生装箱和拆箱
string str = "";
int a = Convert.ToInt32(str); int b =;
IComparable cc = b;//装箱 Console.ReadKey();
}
}
}
3.字典集合
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 字典集合
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(, "");
dic.Add(, "");
dic.Add(, ""); dic[] = "new111"; //foreach (var item in dic.Keys)
//{
// Console.WriteLine(dic[item]);
//}
foreach (KeyValuePair<int,string> kv in dic)
{ Console.WriteLine("{0}----{1}", kv.Key, kv.Value);
} Console.ReadKey();
}
}
}
练习一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 集合练习
{
class Program
{
static void Main(string[] args)
{
//将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中
//最终将两个集合合拼并一个集合,并且奇数显示在左边, 偶数显示在右边。
//Convert.ToBoolean(i % 2); 1--true,0--false List<int> list = new List<int>();
for (int i = ; i < ; i++)
{
list.Add(i);
} List<int> list_ji = new List<int>();
List<int> list_ou = new List<int>(); for (int i = ; i < list.Count; i++)
{
if (Convert.ToBoolean(list[i] % ))
{
list_ji.Add(list[i]);
}
else
list_ou.Add(list[i]);
} Dictionary<int, int> dic = new Dictionary<int, int>();
for (int i = ; i < list_ji.Count; i++)
{
dic.Add(list_ji[i], list_ou[i]);
}
foreach (KeyValuePair<int,int> kv in dic)
{
Console.WriteLine("{0}----{1}",kv.Key,kv.Value);
} Console.ReadKey();
}
}
}
练习二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 集合练习2
{
class Program
{
static void Main(string[] args)
{
//提示用户输入一个字符串,通过foreach循环将用户输入的字符串赋值给一个字符数组
//string ss = "Welcome";
//char [] chars = ss.ToCharArray(); //统计Welcome to china 中每个字符出现的次数,不考虑大小写
string ss = "Welcome to china";
ss = ss.Trim();
ss=ss.Replace(" ","");
ss = ss.ToUpper();
Console.WriteLine(ss);
Dictionary<char, int> dic = new Dictionary<char, int>();
char[] chars = ss.ToArray();
int value=;
for (int i = ; i < chars.Length; i++)
{
if(dic.ContainsKey(chars[i]))
{
dic[chars[i]] = dic[chars[i]] + ;
}
else
{
dic.Add(chars[i], value);
}
} foreach (KeyValuePair<char,int> item in dic)
{
Console.WriteLine("{0}--{1}",item.Key,item.Value); } Console.ReadKey();
}
}
}
C#面向对象14 List泛型集合/装箱和拆箱/字典集合(Dictionary)的更多相关文章
- java - day010 - 基本类型包装,自动装箱和拆箱,日期,集合
基本类型的包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolea ...
- C#基础知识系列二(值类型和引用类型、可空类型、堆和栈、装箱和拆箱)
前言 之前对几个没什么理解,只是简单的用过可空类型,也是知道怎么用,至于为什么,还真不太清楚,通过整理本文章学到了很多知识,也许对于以后的各种代码优化都有好处. 本文的重点就是:值类型直接存储其值,引 ...
- 对象转型、迭代器Iterator、Set集合、装箱与拆箱、基本数据类型与字符串的转换、TreeSet集合与对象
包的声明与定义 需要注意的是,包的声明只能位于Java源文件的第一行. 在实际程序开发过程中,定义的类都是含有包名的: 如果没有显式地声明package语句,创建的类则处于默认包下: 在实际开发中 ...
- C# 程序性能提升篇-1、装箱和拆箱,枚举的ToString浅析
前景提要: 编写程序时,也许你不经意间,就不知不觉的使程序代码,发生了装箱和拆箱,从而降低了效率,不要说就发生那么一次两次,如果说是程序中发生了循环.网络程序(不断请求处理的)等这些时候,减少装箱和拆 ...
- c#基础语言编程-装箱和拆箱
引言 为什么有装箱和拆箱,两者起到什么作用?NET的所有类型都是由基类System.Object继承过来的,包括最常用的基础类型:int, byte, short,bool等等,就是说所有的事物都是对 ...
- Java自动装箱和拆箱
jdk5.0之后,在基本数据类型封装类之间增加了自动装箱和拆箱的功能,其实“自动”的实现很简单,只是将装箱和拆箱通过编译器,进行了“自动补全”,省去了开发者的手动操作. 而进行封装类与对应基本数据类型 ...
- C#装箱和拆箱。
装箱:值类型-->引用类型. 拆箱:引用类型-->值类型 装箱:把值类型拷贝一份到堆里.反之拆箱. 具有父子关系 是拆装箱的条件之一. 所以: class Program { static ...
- 基础系列(4)—— C#装箱和拆箱
一 装箱和拆箱的概念 装箱是将值类型转换为引用类型 : 拆箱是将引用类型转换为值类型 : 值类型:包括原类型(Sbyte.Byte.Short.Ushort.Int.Uint.Long.Ulong.C ...
- 全面理解java自动装箱和拆箱(转)
自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象.自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接. 如 ...
随机推荐
- PostgreSQL 当月最后一天的工作日 , 计算日期是星期几
可以用pg自带函数select extract(dow from current_date),之所以没用主要是展示一下通过数学方法计算日期的原理. drop function if exists ge ...
- 处理输入为非对角阵的Clustering by fast search and find of density peak代码
Clustering by fast search and find of density peak. Alex Rodriguez, Alessandro Laio 是发表在Science上的一篇很 ...
- 阿里云轻应用服务器配置Ubuntu的JDK、Tmocat、Mysql和Redis
1.与服务器建立连接(达到效果:XShell和Xftp均可连接到服务器) 阿里云管理控制台提供的三种建立服务器连接方式: 使用浏览器发起安全连接(推荐) 客户端使用密钥进行连接 客户端使用账号密码 ...
- itchat库微信自动回复祝福语
过年了,之前看到一些python文章介绍用itchat自动回复微信,我自己就写了一个. 官方文档https://itchat.readthedocs.io/zh/latest/,这个库挺简洁的,对着接 ...
- Spring+Ibatis开发
Spring+Ibatis开发:1.首先回忆Spring+Hibernate开发:那么时候我们是先加入的Spring,然后在加入Hibernate支持包的,而此时我们Spring+Ibatis开发,原 ...
- 一百一十二:CMS系统之前台用户模型
安装shortuuid用于前台用户的主键:pip install shortuuid 创建模型 from datetime import datetimeimport enumfrom werkzeu ...
- CPU-内存-IO-网络调优
一.关于CPU 中央处理器调优 1. CPU处理方式: 批处理,顺序处理请求.(切换次数少,吞吐量大) 分时处理.(如同"独占",吞吐量小)(时间片,把请求分为一个一个的时间片,一 ...
- centos7 忘记mysql root登录密码
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的状态下,其他的用户也可以任意地登录和 ...
- highway network及mnist数据集测试
先说结论:没经过仔细调参,打不开论文所说代码链接(fq也没打开),结果和普通卷积网络比较没有优势.反倒是BN对网络起着非常重要的作用,达到了99.17%的测试精度(训练轮数还没到过拟合). 论文为&l ...
- 【并行计算-CUDA开发】CUDA shared memory bank 冲突
CUDA SHARED MEMORY shared memory在之前的博文有些介绍,这部分会专门讲解其内容.在global Memory部分,数据对齐和连续是很重要的话题,当使用L1的时候,对齐问题 ...