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)的更多相关文章

  1. java - day010 - 基本类型包装,自动装箱和拆箱,日期,集合

    基本类型的包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolea ...

  2. C#基础知识系列二(值类型和引用类型、可空类型、堆和栈、装箱和拆箱)

    前言 之前对几个没什么理解,只是简单的用过可空类型,也是知道怎么用,至于为什么,还真不太清楚,通过整理本文章学到了很多知识,也许对于以后的各种代码优化都有好处. 本文的重点就是:值类型直接存储其值,引 ...

  3. 对象转型、迭代器Iterator、Set集合、装箱与拆箱、基本数据类型与字符串的转换、TreeSet集合与对象

      包的声明与定义 需要注意的是,包的声明只能位于Java源文件的第一行. 在实际程序开发过程中,定义的类都是含有包名的: 如果没有显式地声明package语句,创建的类则处于默认包下: 在实际开发中 ...

  4. C# 程序性能提升篇-1、装箱和拆箱,枚举的ToString浅析

    前景提要: 编写程序时,也许你不经意间,就不知不觉的使程序代码,发生了装箱和拆箱,从而降低了效率,不要说就发生那么一次两次,如果说是程序中发生了循环.网络程序(不断请求处理的)等这些时候,减少装箱和拆 ...

  5. c#基础语言编程-装箱和拆箱

    引言 为什么有装箱和拆箱,两者起到什么作用?NET的所有类型都是由基类System.Object继承过来的,包括最常用的基础类型:int, byte, short,bool等等,就是说所有的事物都是对 ...

  6. Java自动装箱和拆箱

    jdk5.0之后,在基本数据类型封装类之间增加了自动装箱和拆箱的功能,其实“自动”的实现很简单,只是将装箱和拆箱通过编译器,进行了“自动补全”,省去了开发者的手动操作. 而进行封装类与对应基本数据类型 ...

  7. C#装箱和拆箱。

    装箱:值类型-->引用类型. 拆箱:引用类型-->值类型 装箱:把值类型拷贝一份到堆里.反之拆箱. 具有父子关系 是拆装箱的条件之一. 所以: class Program { static ...

  8. 基础系列(4)—— C#装箱和拆箱

    一 装箱和拆箱的概念 装箱是将值类型转换为引用类型 : 拆箱是将引用类型转换为值类型 : 值类型:包括原类型(Sbyte.Byte.Short.Ushort.Int.Uint.Long.Ulong.C ...

  9. 全面理解java自动装箱和拆箱(转)

    自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象.自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接. 如 ...

随机推荐

  1. git上传超过100m大文件

    1.git出错如下错误时 执行如下可解决错误: git rm --cache '大文件路径' git commit --amend -CHEAD git push 2.当必须上传大文件时.需借助git ...

  2. JS基础_数组的遍历

    遍历:将数组中所有的元素都取出来 1.for循环 var arr = ["1","2","3"]; for(let i=0;i<arr ...

  3. swoole入门简介

    原文:https://www.cnblogs.com/dormscript/p/4811921.html 本文主要记录一下学习swoole的过程.填过的坑以及swoole究竟有多么强大! 首先说一下对 ...

  4. ElasticSearch1:基本概念

    ElasticSearch的基本概念 es基本概念: Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档,用JSON作为文档序列化的格式 NRT:Nearly Real Time ...

  5. tp5 模型关联,多表联查实用方法

    1.模型中建立关联关系 public function goods(){ return $this->belongsTo('app\common\model\goods\Goods', 'goo ...

  6. spring + mybatis + mysql/oracle开发

    1)创建一个spring-mybatis-mysql这么一个javaweb或java工程 2)导入spring-ioc,spring-aop,spring-transaction,mybatis,c3 ...

  7. 侧方、s弯道、坡起相关

    侧方: 方向盘上端对准路中箭头直行,当前面箭头头部尖角刚刚消失,停车,挂倒档,倒,当箭头尾部快要消失时右打死,侧身看左后视镜(这时可以稍微踩一下离合控制速度为低速),当出现库底角回正,坐直,当左侧第一 ...

  8. Restful 风格

    大家在做Web开发的过程中,method常用的值是get和post. 可事实上,method值还可以是put和delete等等其他值.既然method值如此丰富,那么就可以考虑使用同一个url,但是约 ...

  9. [Kaggle] How to kaggle?

    成立于2010年的Kaggle是一个进行数据发掘和预测竞赛的在线平台.与Kaggle合作之后,一家公司可以提供一些数据,进而提出一个问题,Kaggle网站上的计算机科学家和数学家,也就是现在所说的数据 ...

  10. openstack部署neutron

    controller 1.创建数据库并设置权限 mysql -u root -p0330 CREATE DATABASE neutron; GRANT ALL PRIVILEGES ON neutro ...