c# List AddRange
https://msdn.microsoft.com/zh-cn/library/z883w3dc(v=vs.80).aspx
List 中会保留集合中元素的顺序。
如果新的 Count(当前 Count 加上集合的大小)大于 Capacity,则会通过自动重新分配内部数组增大 List 的容量以容纳新元素,并在添加新元素之前将现有元素复制到新数组中。
如果 List 可以在不增加 Capacity 的情况下容纳新元素,则此方法是 O(n) 运算,其中 n 是要添加的元素数。如果需要增加此容量以容纳新元素,则此方法变为 O(n + m) 运算,其中 n 是要添加的元素数,m 是 Count。
List.AddRange :将指定集合的元素添加到 List 的末尾。
using System;
using System.Collections.Generic; public class Example
{
public static void Main()
{
string[] input = { "Brachiosaurus",
"Amargasaurus",
"Mamenchisaurus" }; List<string> dinosaurs = new List<string>(input); Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\nAddRange(dinosaurs)");
dinosaurs.AddRange(dinosaurs); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\nRemoveRange(2, 2)");
dinosaurs.RemoveRange(, ); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} input = new string[] { "Tyrannosaurus",
"Deinonychus",
"Velociraptor"}; Console.WriteLine("\nInsertRange(3, input)");
dinosaurs.InsertRange(, input); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
string[] output = dinosaurs.GetRange(, ).ToArray(); Console.WriteLine();
foreach( string dinosaur in output )
{
Console.WriteLine(dinosaur);
}
}
} /* This code example produces the following output: Capacity: 3 Brachiosaurus
Amargasaurus
Mamenchisaurus AddRange(dinosaurs) Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus RemoveRange(2, 2) Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus InsertRange(3, input) Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus output = dinosaurs.GetRange(2, 3).ToArray() Amargasaurus
Tyrannosaurus
Deinonychus
*/
c# List AddRange的更多相关文章
- Add和AddRange的区别
Add和AddRange区别 Add和AddRange Add:将指定的对象添加到……中 AddRange:向……末尾,添加数组 - 在群体操作时,使用AddRange取代Add 用AddRange可 ...
- C# AddRange为数组添加多个元素的代码
将代码过程中重要的代码片段做个收藏,下面代码段是关于C# AddRange为数组添加多个元素的代码,希望对小伙伴有所用处.ArrayList ab = new ArrayList();ab.Add(& ...
- HttpWebRequest.AddRange 支持long类型
很久很久以前,在哪个FAT32格式还流行的年代,文件大小普遍还没超过4G的年代,.Net已经出来了. 而那时候.Net实现的HTTP断点续传协议,还没预料到如此普及(我猜的).那时候的HttpWebR ...
- AddRange 取代 Add
cmd.Parameters的Add方法是增加一个参数,增加多个参数的的时候使用一个foreach循环,cmd.Parameters的AddRange方法是增加一个参数的数组. Add:将指定的对象添 ...
- Add和AddRange的使用
Add 是每次将单个元素添加到集合里面 AddRange可以一次性添加多个元素到集合里面 AddRange例子: public static int ExecuteCommand(st ...
- 【译】第42节---EF6-DbSet.AddRange & DbSet.RemoveRange
原文:http://www.entityframeworktutorial.net/entityframework6/addrange-removerange.aspx EF 6中的DbSet引入了新 ...
- 批量 1 insert into select 2 sqldataadapter.update 3 SELECT INTO FROM (要求目标表不存在) 4 AddRange(ef) 5 MySqlBulkLoader.Load() 6 BatchInsert 7 insert bulk
insert into a(col1,col2) select top 1 '1','2' from a union select top 1 '3','4' from a SELECT IN ...
- C#集合中的Add与AddRange方法
C#.NET的集合主要位于System.Collections和System.Collections.Generic(泛型)这两个namespace中. 1.System.Collections 比如 ...
- AutoDetectChangesEnabled及AddRange解决EF插入的性能问题
转自:http://www.cnblogs.com/nianming/archive/2013/06/07/3123103.html#2699851 记录下. 园友莱布尼茨写了一篇<Entity ...
随机推荐
- ubuntu中maven建的web项目不能将project facet设置为 dynamic web module 3.0
核心参考:maven 不能设置为web3.0人解决方法 error:Description Resource Path Location Type Cannot change ...
- PHP漏洞全解
针对PHP的网站主要存在下面几种攻击方式: 1.命令注入(Command Injection) 2.eval注入(Eval Injection) 3.客户端脚本攻击(Script Insertion) ...
- MongoDB安装,配置
安装 cd /usr/local/srcwget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.6.tgztar z ...
- Scala高阶函数示例
object Closure { def function1(n: Int): Int = { val multiplier = (i: Int, m: Int) => i * m multip ...
- 使用IDEA进行远程调试
虽然很早以前就只有Eclipse和IDEA都支持远程调试功能的,但是基本没怎么使用过,今天因为紧急处理一个问题,而本地环境搭建起来比较麻烦,所以就使用了IDEA的远程调试功能.因此写一篇文章记录一下. ...
- CF 321B Kefa and Company(贪心)
题目链接: 传送门 Kefa and Company time limit per test:2 second memory limit per test:256 megabytes Desc ...
- Linux进程关闭和后台运行解析
1.问题背景 Java是跨平台的,大部分程序也都是在Linux服务器上运行的.但是很多朋友其实对服务器了解并不多,对相关知识也是一知半解.很多概念可能知道,但是并不十分清楚,仅仅是基本运用.可能很多新 ...
- Git连接到自己的GitHub仓库
1.配置本地git $git config --global user.name "xxx" $git config --global user.email "xxxxx ...
- JS-百钱买百鸡案例-for循环制作
<html> <head> <meta charset="utf-8"/> <title></title> <sc ...
- 9.13 JS循环
循环:循环操作某一个功能(执行某段代码) 四要素: 循环初始值 循环条件 状态改变 循环体 for 穷举 迭代 i++;等价于i=i+1; ++I;等价于 ...