c#查找列表指定元素的索引_在集合的指定索引处插入元素 在C#中
c#查找列表指定元素的索引
Given a Collection<T> of Integer and we have to insert an element at given index.
給定Integer的Collection <T>,我們必須在給定的索引處插入一個(gè)元素。
To insert an element in Collection<T>, we use Insert() method, it accepts two parameters index and item, where index is an integer type and item is of T type i.e. the type of the Collection<T>.
要在Collection <T>中插入元素,我們使用Insert()方法 ,它接受兩個(gè)參數(shù)index和item ,其中index是整數(shù)類型,而item是T類型,即Collection <T>的類型。
Syntax:
句法:
public void Insert (int index, T item);Note: Insert() method may return exception (ArgumentOutOfRangeException), if index is either less than 0 or greater than the count.
注意:如果index小于0或大于count,則Insert()方法可能會(huì)返回異常( ArgumentOutOfRangeException )。
用C#代碼在Collection <T>中插入元素 (C# code to insert an element in the Collection<T>)
using System; using System.Collections.Generic; using System.Collections.ObjectModel;class IncludeHelp {public static void Main(){// declaring a collection of integersCollection<int> iColl = new Collection<int>();// adding elements to the collectioniColl.Add(100);iColl.Add(200);iColl.Add(300);iColl.Add(400);// displaying total number of elementsConsole.WriteLine("Total number of elements: " + iColl.Count);// displaying all elements foreach (int ele in iColl){Console.WriteLine(ele);}// now inserting 900 at 3rd indexiColl.Insert(3, 900);// displaying total number of elementsConsole.WriteLine("Total number of elements: " + iColl.Count);// displaying all elements foreach (int ele in iColl){Console.WriteLine(ele);}} }Output
輸出量
Total number of elements: 4 100 200 300 400 Total number of elements: 5 100 200 300 900 400Displaying exception
顯示異常
Here, we will insert an element at -1 index that will generate "ArgumentOutOfRangeException" exception.
在這里,我們將在-1索引處插入一個(gè)元素,該元素將生成“ ArgumentOutOfRangeException”異常。
using System; using System.Collections.Generic; using System.Collections.ObjectModel;class IncludeHelp {public static void Main(){// declaring a collection of integersCollection<int> iColl = new Collection<int>();// adding elements to the collectioniColl.Add(100);iColl.Add(200);iColl.Add(300);iColl.Add(400);// displaying total number of elementsConsole.WriteLine("Total number of elements: " + iColl.Count);// displaying all elements foreach (int ele in iColl){Console.WriteLine(ele);}// now inserting 900 at -1 index// that will generate exceptioniColl.Insert(-1, 900);// displaying total number of elementsConsole.WriteLine("Total number of elements: " + iColl.Count);// displaying all elements foreach (int ele in iColl){Console.WriteLine(ele);}} }Output
輸出量
Total number of elements: 4 100 200 300 400Unhandled Exception: System.ArgumentOutOfRangeException: Index must be within the bounds of the List. Parameter name: indexat System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00029] in <65984520577646ec9044386ec4a7b3dd>:0 at System.Collections.ObjectModel.Collection`1[T].Insert (System.Int32 index, T item) [0x00026] in <65984520577646ec9044386ec4a7b3dd>:0 at IncludeHelp.Main () [0x0007f] in <d658237a48934373b441d64c47cad823>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Index must be within the bounds of the List. Parameter name: indexat System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00029] in <65984520577646ec9044386ec4a7b3dd>:0at System.Collections.ObjectModel.Collection`1[T].Insert (System.Int32 index, T item)[0x00026] in <65984520577646ec9044386ec4a7b3dd>:0at IncludeHelp.Main () [0x0007f] in <d658237a48934373b441d64c47cad823>:0翻譯自: https://www.includehelp.com/dot-net/inserting-an-element-at-specified-index-in-a-collection-t-in-csharp.aspx
c#查找列表指定元素的索引
總結(jié)
以上是生活随笔為你收集整理的c#查找列表指定元素的索引_在集合的指定索引处插入元素 在C#中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: math 计算float_Java Ma
- 下一篇: c#给定二维数组按升序排序_在数组中按升