-
C# |在数组列表末尾添加元素
C# |在数组列表末尾添加元素
原文:https://www . geesforgeks . org/c-sharp-将元素添加到数组末尾列表/
AddRange(ICollection)方法用于将 ICollection 的元素添加到数组列表的末尾。或者换句话说,这个方法用于将其他集合中的多个元素添加到数组列表中。在这里,元素被定义为基元或非基元类型。
语法:
public virtual void AddRange (ICollection col);
这里,列是一个 ICollection,它的元素应该被添加到数组列表的末尾。集合本身不能为 null,但它可以包含 null 元素。
例外:
- 如果列的值为空,则该方法将给出参数空异常。
- 如果数组列表是只读的,或者数组列表有固定的大小,这个方法将给出 NotSupportedException
下面给出了一些例子,以便更好地理解实现:
例 1:
// C# program to illustrate the AddRange() Method
using System;
using System.Collections;
class GFG {
// Main method
public static void Main()
{
// Creates and initializes a new ArrayList
ArrayList mylist = new ArrayList();
mylist.Add("C# ");
mylist.Add("DSA ");
mylist.Add("Java ");
mylist.Add("CPP");
// Creates and initializes a new Queue
Queue mydata = new Queue();
mydata.Enqueue("C ");
mydata.Enqueue("Python ");
mydata.Enqueue("HTML ");
mydata.Enqueue("CSS ");
mydata.Enqueue("JavaScript ");
mydata.Enqueue("Ruby");
// Displays the ArrayList and the Queue.
Console.Write("The original ArrayList: ");
Data(mylist);
Console.Write("The original Queue: ");
Data(mydata);
// Copies the elements of the queue to the end of the ArrayList.
mylist.AddRange(mydata);
// Displays the new ArrayList.
Console.Write("The new ArrayList is: ");
Data(mylist);
}
// method to display the ArrayList
// and Queue elements
public static void Data(IEnumerable mylis)
{
foreach(string str in mylis)
Console.Write("{0}", str);
Console.WriteLine();
}
}
输出:
The original ArrayList: C# DSA Java CPP
The original Queue: C Python HTML CSS JavaScript Ruby
The new ArrayList is: C# DSA Java CPPC Python HTML CSS JavaScript Ruby
例 2:
// C# program to illustrate
// the AddRange() Method
using System;
using System.Collections;
public class GFG {
// Main method
public static void Main()
{
// Creates and initializes a new ArrayList.
ArrayList mylist = new ArrayList();
mylist.Add(5);
mylist.Add(10);
mylist.Add(15);
mylist.Add(20);
mylist.Add(25);
// Creates and initializes a new ArrayList.
ArrayList mydata = new ArrayList();
mydata.Add(30);
mydata.Add(35);
mydata.Add(40);
mydata.Add(45);
mydata.Add(50);
// Displays both ArrayList.
Console.WriteLine("The ArrayList 1: ");
foreach(int i in mylist)
{
Console.WriteLine(i);
}
Console.WriteLine("The ArrayList 2: ");
foreach(int j in mydata)
{
Console.WriteLine(j);
}
// Copies the elements of the mydata
// to the end of the mylist
mylist.AddRange(mydata);
// Displays the new ArrayList.
Console.WriteLine("The new ArrayList is :");
for (int k = 0; k < mylist.Count; k++)
Console.WriteLine(mylist[k]);
}
}
输出:
The ArrayList 1:
5
10
15
20
25
The ArrayList 2:
30
35
40
45
50
The new ArrayList is :
5
10
15
20
25
30
35
40
45
50
参考:
- https://docs . Microsoft . com/en-us/dotnet/API/system . collections . ArrayList . addrange?view = net framework-4 . 7 . 2 # System _ Collections _ ArrayList _ AddRange _ System _ Collections _ ICollection _
版权属于:月萌API www.moonapi.com,转载请注明出处
本文链接:https://www.moonapi.com/news/27791.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数