C# List 详解二

目录

5.Clear()    

6.Contains(T)    

7.ConvertAll(Converter)    ,toutput>

8.CopyTo(Int32, T[], Int32, Int32)    

9.CopyTo(T[])    

10.CopyTo(T[], Int32)    


C# List 详解一

1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.BinarySearch(T),

C# List 详解二

5.Clear(),6.Contains(T),7.ConvertAll(Converter),8.CopyTo(Int32, T[], Int32, Int32),9.CopyTo(T[]),10.CopyTo(T[], Int32)

C# List 详解三

11.Equals(Object),12.Exists(Predicate),13.Find(Predicate),14.FindAll(Predicate),15.FindIndex(Int32, Int32, Predicate),16.FindIndex(Int32, Predicate),17.FindIndex(Predicate)  

C# List 详解四

18.FindLast(Predicate),19.FindLastIndex(Int32, Int32, Predicate),20.FindLastIndex(Int32, Predicate),21.FindLastIndex(Predicate),22.ForEach(Action),23.GetEnumerator(),24.GetHashCode(),25.GetRange(Int32, Int32) 

C# List 详解五

26.GetType(),27.IndexOf(T),28.IndexOf(T, Int32),29.IndexOf(T, Int32, Int32),30.Insert(Int32, T),31.InsertRange(Int32, IEnumerable),32.LastIndexOf(T),33.LastIndexOf(T, Int32),34.LastIndexOf(T, Int32, Int32)    

C# List 详解六

35.MemberwiseClone(),36.Remove(T),37.RemoveAll(Predicate),38.RemoveAt(Int32),39.RemoveRange(Int32, Int32),40.Reverse(),41.Reverse(Int32, Int32)    

C# List 详解七

42.Sort(),43.ToArray(),44.ToString(),45.TrimExcess(),46.TrueForAll(Predicate) 

C# List 详解一_熊思宇的博客-CSDN博客

C# List 详解三_熊思宇的博客-CSDN博客

C# List 详解四_熊思宇的博客-CSDN博客

C# List 详解五_熊思宇的博客-CSDN博客

C# List 详解六_熊思宇的博客-CSDN博客

C# List 详解七_熊思宇的博客-CSDN博客

5.Clear()    

从 List<T> 中移除所有元素。List 中比较常用的方法,用来清空 List 的所有元素。

public void Clear ();

案例:

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<string> dinosaurs = new List<string>();dinosaurs.Add("Pachycephalosaurus");dinosaurs.Add("Parasauralophus");dinosaurs.Add("Amargasaurus");Console.WriteLine(dinosaurs.Count);dinosaurs.Clear();Console.WriteLine(dinosaurs.Count);Console.ReadKey();}}
}

运行:

6.Contains(T)    

确定某元素是否在 List<T> 中。

public bool Contains (T item);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

返回
Boolean
如果在 true 中找到 item,则为 List<T>;否则为 false。

案例:

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<string> dinosaurs = new List<string>();dinosaurs.Add("Pachycephalosaurus");dinosaurs.Add("Parasauralophus");dinosaurs.Add("Amargasaurus");if (dinosaurs.Contains("Amargasaurus")){Console.WriteLine("包含");}else{Console.WriteLine("不包含");}Console.ReadKey();}}
}

运行:

7.ConvertAll<TOutput>(Converter<T,TOutput>)    

将当前 List<T> 中的元素转换为另一种类型,并返回包含已转换元素的列表。

public System.Collections.Generic.List<TOutput> ConvertAll<TOutput> (Converter<T,TOutput> converter);

类型参数
TOutput
目标数组元素的类型。

参数
converter
Converter<T,TOutput>
一个 Converter<TInput,TOutput> 委托,可将每个元素从一种类型转换为另一种类型。

返回
List<TOutput>
目标类型的 List<T>,包含当前 List<T> 中转换后的元素。

案例:

就是将 List 中的每一个元素转换为另一种格式,并返回一个新的 List 

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<int> intList = new List<int>() { 2, 5, 33, 6, 23 };List<string> stringList = intList.ConvertAll(new Converter<int, string>(convertall));Console.WriteLine(string.Join("-", stringList));Console.ReadKey();}static string convertall(int val){return string.Format("'{0}'", val);}}
}

上面代码也可以用 Lambda 表达式去写:

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<int> intList = new List<int>() { 2, 5, 33, 6, 23 };List<string> stringList = intList.ConvertAll(new Converter<int, string>((val) => { return string.Format("'{0}'", val); }));Console.WriteLine(string.Join("-", stringList));Console.ReadKey();}}
}

运行:

8.CopyTo(Int32, T[], Int32, Int32)    

从目标数组的指定索引处开始,将元素的范围从 List<T> 复制到兼容的一维数组。

public void CopyTo (int index, T[] array, int arrayIndex, int count);

参数
index
Int32
复制即从源 List<T> 中从零开始的索引开始。

array
T[]
一维 Array,它是从 List<T> 复制的元素的目标。 Array 必须具有从零开始的索引。

arrayIndex
Int32
array 中从零开始的索引,从此处开始复制。

count
Int32
要复制的元素数。

由于引用类型在改变值后,其他的数组中的值也会改变,所以有时候,数组不得不用拷贝的方式,下面用两个案例来演示 CopyTo 的用法。

案例1:完整的拷贝

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<int> intList = new List<int>() { 2, 5, 33, 6, 23 };int[] list = new int[intList.Count];intList.CopyTo(0, list, 0, intList.Count);Console.WriteLine(string.Join("-", list));Console.ReadKey();}}
}

运行:

案例2:拷贝一部分

在下面这几个参数中,第一个参数 2 表示从 intList 中的下标 2 开始,即 33 开始,第三个参数 0 表示 list 数组中,从 0 这个下标开始赋值,往后赋值2个参数

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<int> intList = new List<int>() { 2, 5, 33, 6, 23, 45,36,38 };int[] list = new int[intList.Count];intList.CopyTo(2, list, 0, 3);Console.WriteLine(string.Join("-", list));Console.ReadKey();}}
}

 运行:

9.CopyTo(T[])    

从目标数组的开头开始,将整个 List<T> 复制到兼容的一维数组。

public void CopyTo (T[] array);

参数
array
T[]
一维 Array,它是从 List<T> 复制的元素的目标。 Array 必须具有从零开始的索引。

案例:

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<int> intList = new List<int>() { 2, 5, 33, 6, 23, 45,36,38 };int[] list = new int[intList.Count];intList.CopyTo(list);Console.WriteLine(string.Join("-", list));Console.ReadKey();}}
}

运行:

10.CopyTo(T[], Int32)    

从目标数组的指定索引处开始,将整个 List<T> 复制到兼容的一维数组。

public void CopyTo (T[] array, int arrayIndex);

参数
array
T[]
一维 Array,它是从 List<T> 复制的元素的目标。 Array 必须具有从零开始的索引。

arrayIndex
Int32
array 中从零开始的索引,从此处开始复制。

案例:

这里和上一节的用法差不多,只是多了一个 arrayIndex

using System;
using System.Collections.Generic;namespace ListTest
{internal class Program{static void Main(string[] args){List<int> intList = new List<int>() { 2, 5, 33, 6, 23, 45,36,38 };int[] list = new int[intList.Count + 5];intList.CopyTo(list, 5);Console.WriteLine(string.Join("-", list));Console.ReadKey();}}
}

运行:

第 2 / 7  篇  End

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/6279.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pytorch创建和操作tensor

import torch import numpy as np### 1. 由函数创建 x torch.zeros(5, 3, dtypetorch.int64) # 指定数据类型 print(x.dtype) x torch.zeros(5, 3) # 默认数据类型为torch.float32 print(x.dtype)x torch.rand(5, 3)x torch.torch.ones(10,2,3) x torch.empty(5, 3)# Re…

Matlab的GUI设计

文章目录 AppDesigner各个版本的特点mlapp文件基本格式AppDesigner的回调函数常见控件的属性MVC模式MVC模式设计GUIMVC简单使用 其他让app designer置顶将Guide的GUI导出为m文件将app编译为exe将app中的多个控件组合在一起 AppDesigner 20200328 各个版本的特点 在2017b版本中…

【JavaEE】Spring中注解的方式去获取Bean对象

【JavaEE】Spring的开发要点总结&#xff08;3&#xff09; 文章目录 【JavaEE】Spring的开发要点总结&#xff08;3&#xff09;1. 属性注入1.1 Autowired注解1.2 依赖查找 VS 依赖注入1.3 配合Qualifier 筛选Bean对象1.4 属性注入的优缺点 2. Setter注入2.1 Autowired注解2.2…

21matlab数据分析牛顿插值(matlab程序)

1.简述 一、牛顿插值法原理 1.牛顿插值多项式   定义牛顿插值多项式为&#xff1a; N n ( x ) a 0 a 1 ( x − x 0 ) a 2 ( x − x 0 ) ( x − x 1 ) ⋯ a n ( x − x 0 ) ( x − x 1 ) ⋯ ( x − x n − 1 ) N_n\left(x\right)a_0a_1\left(x-x_0\right)a_2\left(x-x_0\…

NLP masked_tokens[]、token_masks[]是什么?

1、masked_tokens[]、token_masks[]介绍 masked_tokens和token_masks两个列表用于存储mask处理后的token&#xff08;分词&#xff09;结果和对应的mask标志。 masked_tokens列表存储经过mask处理后的分词结果。 token_masks列表存储与每个分词结果对应的mask标志。 2、示例…

Electron运行时报错:浏览器报错Uncaught ReferenceError: require is not defined

这个错误通常发生在浏览器环境中使用了Node.js的模块化语法。由于浏览器不支持直接使用require关键字&#xff0c;所以会报错。 解决这个问题的一种方法是使用Webpack等工具将你的代码打包成浏览器可执行的文件。这样可以将require语法转换为浏览器可识别的语法。 另外&#…

SpringBoot 如何使用 EmbeddedDatabaseBuilder 进行数据库集成测试

SpringBoot 如何使用 EmbeddedDatabaseBuilder 进行数据库集成测试 在开发 SpringBoot 应用程序时&#xff0c;我们通常需要与数据库进行交互。为了确保我们的应用程序在生产环境中可以正常工作&#xff0c;我们需要进行数据库集成测试&#xff0c;以测试我们的应用程序是否能…

剑指offer61.扑克牌中的顺子

我的想法非常简单&#xff0c;就是先给数组排序&#xff0c;然后统计里面有几个0&#xff0c;然后遍历数组&#xff0c;如果是0或者比后面一个数小1就直接进入下一次循环&#xff0c;如果比后面一个数小2&#xff0c;就用掉一个0&#xff0c;0的数量减1&#xff0c;如果比后面的…

Pycharm----导入库文件夹不在py文件的目录下

问题描述&#xff1a; 想在不同目录下导入根目录的包&#xff0c;直接写会报错。如下边object_detect.py在function文件夹下&#xff0c;导入包默认在这个文件下&#xff0c;但我想导入根目录models和utils下的包 解决方法&#xff1a; 将根目录设置为源代码根目录&#xff0…

【OC总结 面向对象 + 内存管理 + runtime】

文章目录 前言面向对象1.1 一个NSObject对象占用多少内存&#xff1f;1.2 iOS的继承链 & 对象的指针指向了哪里&#xff1f;1.3 OC的类的信息存放在哪里&#xff1f;-isa指针1.4 isMemberOfClass & isKindOfClass Runtime1.4 讲一下OC的消息机制1.5 消息转发机制流程1.…

【指针和数组笔试题(1)】详解指针、数组笔试题

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言整型数组字符数组第一组题第二组题第三组题 总结 前言 在计算之前要了解基本概念&#xff1a; 数组名的理解 数组名是数组首元素的地址 有两个例外 1.sizeof(…

Linux网络基础 — 数据链路层

目录 数据链路层 认识以太网 局域网转发的原理 认识以太网的MAC报头 以太网帧格式 认识MAC地址 对比理解MAC地址和IP地址 基于MAC帧协议再次谈一谈局域网转发的原理 认识MTU MTU对IP协议的影响 MTU对UDP协议的影响 MTU对于TCP协议的影响 ARP协议 ARP协议的作用 …

Xcode 15 beta 4 (15A5195m) - Apple 平台 IDE

Xcode 15 beta 4 (15A5195m) - Apple 平台 IDE IDE for iOS/iPadOS/macOS/watchOS/tvOS/visonOS 请访问原文链接&#xff1a;https://sysin.org/blog/apple-xcode-15/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org visonOS …

SpringBoot整合SpringCloudStream3.1+版本的Kafka死信队列

SpringBoot整合SpringCloudStream3.1版本的Kafka死信队列 上一篇直通车 SpringBoot整合SpringCloudStream3.1版本Kafka 实现死信队列步骤 添加死信队列配置文件&#xff0c;添加对应channel通道绑定配置对应的channel位置添加重试配置 结果 配置文件 Kafka基本配置&#…

C++ deque/queue/stack的底层原理

deque容器的存储结构 和 vector 容器采用连续的线性空间不同&#xff0c;deque 容器存储数据的空间是由一段一段等长的连续空间构成&#xff0c;各段空间之间并不一定是连续的&#xff0c;可以位于在内存的不同区域。 deque采用一块所谓的map数组&#xff08;注意&#xff0c…

LeetCode 0874. 模拟行走机器人:哈希表模拟

【LetMeFly】874.模拟行走机器人&#xff1a;哈希表模拟 力扣题目链接&#xff1a;https://leetcode.cn/problems/walking-robot-simulation/ 机器人在一个无限大小的 XY 网格平面上行走&#xff0c;从点 (0, 0) 处开始出发&#xff0c;面向北方。该机器人可以接收以下三种类…

rabbitmq模块启动报java.net.SocketException: socket closed的解决方法

问题 最近在接手一个项目时&#xff0c;使用的是spring-cloud微服务构架&#xff0c;mq消息消费模块是单独一个模块&#xff0c;但启动这个模块一直报如下错误&#xff1a; java.net.SocketException: socket closed 这个错误是这个模块注册不到nacos报的错&#xff0c;刚开…

day34-Animated Countdown(动画倒计时)

50 天学习 50 个项目 - HTMLCSS and JavaScript day34-Animated Countdown&#xff08;动画倒计时&#xff09; 效果 index.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta name"viewport&q…

态势标绘专题介绍

介绍 这个专栏是专门针对基于Cesium来实现态势标绘的专题专栏,专栏主要实现了30余种态势几何形状的标绘和编辑、文本的标绘和编辑、图片的标绘和编辑以及简单模型的标绘,同时支持标绘结果的导出以及导入。包括最终编写成的一个完整的Vue3.2+TS+Cesium1.107.2的标绘组件。专栏…

C#仿热血江湖

目录 1 GClass10 1.1 定义属性 1.2 int method 1.3 method 1.4 Byte method GClass0 定义属性private byte[] byte_0; private byte[] byte_1;