百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

电脑绝技教你22天学精Csharp之第二十天基础加强总复习 补充5

bigegpt 2024-08-09 11:09 2 浏览

16、集合复习

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Collections;

namespace _16_集合复习

{

class Program

{

static void Main(string[] args)

{

// ArrayList list = new ArrayList();

//// list.Add()

// Hashtable ht = new Hashtable();

// //ht.Add()

// List<int> list = new List<int>();

//list.Add(); 添加单个元素

//list.AddRange();添加集合

//list.Insert();插入

//list.InsertRange();插入集合

//list.Remove();移除

//list.RemoveAt();根据下标移除

//list.RemoveRange();移除一定范围的元素

//list.Contains();//判断是否包含

// list.RemoveAll()

//Dictionary<int, string> dic = new Dictionary<int, string>();

//dic.Add(1,"张三");

//dic.Add(2,"李四");

//dic.Add(3, "颜世伟");

//dic.Add(4, "杀马特");

//dic[4] = "还是杀马特";

//foreach (KeyValuePair<int,string> kv in dic)

//{

// Console.WriteLine("{0}---{1}",kv.Key,kv.Value);

//}

//Console.ReadKey();

//dic.ContainsKey();

}

}

}

17集合练习

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _17集合练习

{

class Program

{

static void Main(string[] args)

{

#region 集合练习1

//案例:把分拣奇偶数的程序用泛型实现。int[] nums={1,2,3,4,5,6,7,8,9};奇数在左边 偶数在右边

//int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//List<int> listJi = new List<int>();

//List<int> listOu = new List<int>();

//for (int i = 0; i < nums.Length; i++)

//{

// if (nums[i] % 2 == 0)

// {

// listOu.Add(nums[i]);

// }

// else

// {

// listJi.Add(nums[i]);

// }

//}

//listJi.AddRange(listOu);

//foreach (var item in listJi)

//{

// Console.WriteLine(item);

//}

//Console.ReadKey();

#endregion

#region 集合练习2

//练习1:将int数组中的奇数放到一个新的int数组中返回。

//将数组中的奇数取出来放到一个集合中,最终将集合转换成数组 。

//int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//List<int> listJi = new List<int>();

//for (int i = 0; i < nums.Length; i++)

//{

// if (nums[i] % 2 != 0)

// {

// listJi.Add(nums[i]);

// }

//}

////集合转换成数组

//int[] numsNew = listJi.ToArray();

//foreach (var item in numsNew)

//{

// Console.WriteLine(item);

//}

//Console.ReadKey();

#endregion

#region 集合练习3

//练习2:从一个整数的List<int>中取出最大数(找最大值)。

//集合初始化器

//List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//int max = list[0];

//for (int i = 0; i < list.Count; i++)

//{

// if (list[i] > max)

// {

// max = list[i];

// }

//}

//Console.WriteLine(max);

//Console.ReadKey();

////foreach (var item in list)

////{

//// Console.WriteLine(item);

////}

//Console.ReadKey();

// list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });

//Person p = new Person("李四", 16, '女') { Name = "张三", Age = 19, Gender = '男' };

//p.SayHello();

//Console.ReadKey();

#endregion

#region 集合练习4

//练习:把123转换为:壹贰叁。Dictionary<char,char>

//"1一 2二 3三 4四 5五 6六 7七 8八 9九"

//string str = "1一 2二 3三 4四 5五 6六 7七 8八 9九";

////123 一二三

//Dictionary<char, char> dic = new Dictionary<char, char>();

//string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

//for (int i = 0; i < strNew.Length; i++)

//{

// //1一 strNew[i][0] strNew[i][1]

// dic.Add(strNew[i][0], strNew[i][1]);

//}

//Console.WriteLine("请输入阿拉伯数字");

//string input = Console.ReadLine();

//for (int i = 0; i < input.Length; i++)

//{

// if (dic.ContainsKey(input[i]))

// {

// Console.Write(dic[input[i]]);

// }

// else

// {

// Console.Write(input[i]);

// }

//}

//Console.ReadKey();

#endregion

#region 集合练习5

////练习:计算字符串中每种字符出现的次数(面试题)。 “Welcome to Chinaworld”,不区分大小写,打印“W2”“e 2”“o 3”……

//string s = "Welcome to Chinaworld";

//Dictionary<char, int> dic = new Dictionary<char, int>();

////遍历 s

//for (int i = 0; i < s.Length; i++)

//{

// if (s[i] == ' ')

// {

// continue;

// }

// if (dic.ContainsKey(s[i]))

// {

// dic[s[i]]++;

// }

// else

// {

// dic[s[i]] = 1;

// }

//}

//foreach (KeyValuePair<char,int> kv in dic)

//{

// Console.WriteLine("字母{0}出现了{1}次",kv.Key,kv.Value);

//}

//Console.ReadKey();

#endregion

#region 集合练习5

//案例:两个(List)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。

//List<string> listOne = new List<string>() { "a", "b", "c", "d", "e" };

//List<string> listTwo = new List<string>() { "d", "e", "f", "g", "h" };

//for (int i = 0; i < listTwo.Count; i++)

//{

// if (!listOne.Contains(listTwo[i]))

// {

// listOne.Add(listTwo[i]);

// }

//}

//foreach (var item in listOne)

//{

// Console.WriteLine(item);

//}

//Console.ReadKey();

#endregion

}

}

public class Person

{

public Person(string name, int age, char gender)

{

this.Name = name;

this.Age = age;

this.Gender = gender;

}

public string Name

{

get;

set;

}

public char Gender

{

get;

set;

}

public int Age

{

get;

set;

}

public void SayHello()

{

Console.WriteLine("{0}---{1}---{2}", this.Name, this.Age, this.Gender);

}

}

}

18、静态和非静态的区别

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _18_静态和非静态的区别

{

class Program

{

static void Main(string[] args)

{

Student.Test();

Student.Test();

Student.Test();

Console.ReadKey();

}

}

public class Person

{

private static string _name;

private int _age;

public void M1()

{

}

public static void M2()

{

}

public Person()

{

Console.WriteLine("非静态类构造函数");

}

}

public static class Student

{

static Student()

{

Console.WriteLine("静态类构造函数");

}

public static void Test()

{

Console.WriteLine("我是静态类中的静态方法");

}

// private string _name;

}

}

19、结构和类的区别

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _19_结构和类的区别

{

class Program

{

static void Main(string[] args)

{

//类型

//结构:值类型

//类:引用类型

//声明的语法:class struct

//在类中,构造函数里,既可以给字段赋值,也可以给属性赋值。构造函数是可以重载的

//但是,在结构的构造函数当中,必须只能给字段赋值。

//在结构的构造函数当中,我们需要给全部的字段赋值,而不能去选择的给字段赋值

//调用:

PersonClass pc = new PersonClass();

//结构是否可以New?

//在栈开辟空间 结构new 调用结构的构造函数

PersonStruct ps = new PersonStruct();

ps.M2();

PersonStruct.M1();

Console.ReadKey();

//结构和类的构造函数:

//相同点:不管是结构还是类,本身都会有一个默认的无参数的构造函数

//不同点:当你在类中写了一个新的构造函数之后,那个默认的无参数的构造函数都被干掉了

//但是,在结构当中,如果你写了一个新的构造函数,那么个默认的无参数的构造函数依然在。

//

//如果我们只是单纯的存储数据的话,我们推荐使用结构。

//如果我们想要使用面向对象的思想来开发程序,我们推荐使用我们的Class

//结构并不具备面向对象的特征

// int

}

}

public class PersonClass

{

//字段、属性、方法、构造函数

}

public struct PersonStruct

{

//字段、属性

private string _name;

public string Name

{

get { return _name; }

set { _name = value; }

}

private int _age;

public int Age

{

get { return _age; }

set { _age = value; }

}

private char _gender;

public char Gender

{

get { return _gender; }

set { _gender = value; }

}

public static void M1()

{

Console.WriteLine("我是结构中的静态方法");

}

public void M2()

{

Console.WriteLine("我是结构的非静态方法");

}

public PersonStruct(string name, int age, char gender)

{

//this.Name = name;

//this.Age = age;

//this.Gender = gender;

this._name = name;

this._age = age;

this._gender = gender;

}

//public PersonStruct(string name)

//{

// this._name = name;

//}

}

}

相关推荐

【Docker 新手入门指南】第十章:Dockerfile

Dockerfile是Docker镜像构建的核心配置文件,通过预定义的指令集实现镜像的自动化构建。以下从核心概念、指令详解、最佳实践三方面展开说明,帮助你系统掌握Dockerfile的使用逻...

Windows下最简单的ESP8266_ROTS_ESP-IDF环境搭建与腾讯云SDK编译

前言其实也没啥可说的,只是我感觉ESP-IDF对新手来说很不友好,很容易踩坑,尤其是对业余DIY爱好者搭建环境非常困难,即使有官方文档,或者网上的其他文档,但是还是很容易踩坑,多研究,记住两点就行了,...

python虚拟环境迁移(python虚拟环境conda)

主机A的虚拟环境向主机B迁移。前提条件:主机A和主机B已经安装了virtualenv1.主机A操作如下虚拟环境目录:venv进入虚拟环境:sourcevenv/bin/active(1)记录虚拟环...

Python爬虫进阶教程(二):线程、协程

简介线程线程也叫轻量级进程,它是一个基本的CPU执行单元,也是程序执行过程中的最小单元,由线程ID、程序计数器、寄存器集合和堆栈共同组成。线程的引入减小了程序并发执行时的开销,提高了操作系统的并发性能...

基于网络安全的Docker逃逸(docker)

如何判断当前机器是否为Docker容器环境Metasploit中的checkcontainer模块、(判断是否为虚拟机,checkvm模块)搭配学习教程1.检查根目录下是否存在.dockerenv文...

Python编程语言被纳入浙江高考,小学生都开始学了

今年9月份开始的新学期,浙江省三到九年级信息技术课将同步替换新教材。其中,新初二将新增Python编程课程内容。新高一信息技术编程语言由VB替换为Python,大数据、人工智能、程序设计与算法按照教材...

CentOS 7下安装Python 3.10的完整过程

1.安装相应的编译工具yum-ygroupinstall"Developmenttools"yum-yinstallzlib-develbzip2-develope...

如何在Ubuntu 20.04上部署Odoo 14

Odoo是世界上最受欢迎的多合一商务软件。它提供了一系列业务应用程序,包括CRM,网站,电子商务,计费,会计,制造,仓库,项目管理,库存等等,所有这些都无缝集成在一起。Odoo可以通过几种不同的方式进...

Ubuntu 系统安装 PyTorch 全流程指南

当前环境:Ubuntu22.04,显卡为GeForceRTX3080Ti1、下载显卡驱动驱动网站:https://www.nvidia.com/en-us/drivers/根据自己的显卡型号和...

spark+python环境搭建(python 环境搭建)

最近项目需要用到spark大数据相关技术,周末有空spark环境搭起来...目标spark,python运行环境部署在linux服务器个人通过vscode开发通过远程python解释器执行代码准备...

centos7.9安装最新python-3.11.1(centos安装python环境)

centos7.9安装最新python-3.11.1centos7.9默认安装的是python-2.7.5版本,安全扫描时会有很多漏洞,比如:Python命令注入漏洞(CVE-2015-2010...

Linux系统下,五大步骤安装Python

一、下载Python包网上教程大多是通过官方地址进行下载Python的,但由于国内网络环境问题,会导致下载很慢,所以这里建议通过国内镜像进行下载例如:淘宝镜像http://npm.taobao.or...

centos7上安装python3(centos7安装python3.7.2一键脚本)

centos7上默认安装的是python2,要使用python3则需要自行下载源码编译安装。1.安装依赖yum-ygroupinstall"Developmenttools"...

利用本地数据通过微调方式训练 本地DeepSeek-R1 蒸馏模型

网络上相应的教程基本都基于LLaMA-Factory进行,本文章主要顺着相应的教程一步步实现大模型的微调和训练。训练环境:可自行定义,mac、linux或者window之类的均可以,本文以ma...

【法器篇】天啦噜,库崩了没备份(天啦噜是什么意思?)

背景数据库没有做备份,一天突然由于断电或其他原因导致无法启动了,且设置了innodb_force_recovery=6都无法启动,里面的数据怎么才能恢复出来?本例采用解析建表语句+表空间传输的方式进行...