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的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接. 如 ...
随机推荐
- 1.Json的学习--JSON.stringfy()
1.JSON.parse() JSON.parse() JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScr ...
- React Native布局详解
Flexbox 布局 Flex有两个属性:Container 和 Item flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.采用fle ...
- SpringMVC整合Springfox-Swagger
https://www.jianshu.com/p/ab10860945c3 验证通过 关于Swagger的简介就不占篇幅了... 本文使用的Springfox-Swagger版本为2.8.0 要整合 ...
- Module ngx_http_rewrite_module
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html Directives break if return ...
- 一百二十八:CMS系统之轮播图的编辑和删除功能
编辑 form,继承添加的form 视图 @bp.route('/ubanners/', methods=['POST'])@login_required@permission_required(CM ...
- 在Spring中配置jdbc为什么不能用${username}问题
楼主在spring中配置jdbc时,引用的是dbcp.jar包,在dataSource.properties配置文件中,有mysql用户名,楼主自然的选择了使用username,密码是root, 然后 ...
- charles重发网络请求&模拟慢速网络&过滤网络请求
重发网络请求&模拟慢速网络&过滤网络请求 重发网络请求:后端调试的过程中,一直在客户端进行点点点比较麻烦,此时直接发送请求比较方便查看调试后的结果 模拟慢速网络:用户的网络不能一直是快 ...
- Elasticsearch unassigned 故障排查
1. 故障分析与排查 一个 Elasticsearch 集群至少包括一个节点和一个索引.或者它 可能有一百个数据节点.三个单独的主节点,以及一小打客户端节点--这些共同操作一千个索引(以及上万个分片) ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- 2019牛客暑期多校训练营(第七场)-E Find the median (线段树+离散化 区间为点)
题目链接:https://ac.nowcoder.com/acm/contest/887/E 题意:给出L[i],R[i],每次添加L[i]...R[i],求出此时的中位数. 思路:因为添加的数范围为 ...