c#查找列表指定元素的索引_在集合的指定索引處插入元素 在C#中

c#查找列表指定元素的索引

Given a Collection<T> of Integer and we have to insert an element at given index.

給定Integer的Collection <T>,我們必須在給定的索引處插入一個元素。

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()方法 ,它接受兩個參數index和item ,其中index是整數類型,而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()方法可能會返回異常( 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 integers
Collection<int> iColl = new Collection<int>();
// adding elements to the collection
iColl.Add(100);
iColl.Add(200);
iColl.Add(300);
iColl.Add(400);
// displaying total number of elements
Console.WriteLine("Total number of elements: " + iColl.Count);
// displaying all elements 
foreach (int ele in iColl)
{
Console.WriteLine(ele);
}
// now inserting 900 at 3rd index
iColl.Insert(3, 900);
// displaying total number of elements
Console.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
400

Displaying exception

顯示異常

Here, we will insert an element at -1 index that will generate "ArgumentOutOfRangeException" exception.

在這里,我們將在-1索引處插入一個元素,該元素將生成“ ArgumentOutOfRangeException”異常。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class IncludeHelp
{
public static void Main()
{
// declaring a collection of integers
Collection<int> iColl = new Collection<int>();
// adding elements to the collection
iColl.Add(100);
iColl.Add(200);
iColl.Add(300);
iColl.Add(400);
// displaying total number of elements
Console.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 exception
iColl.Insert(-1, 900);
// displaying total number of elements
Console.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
Unhandled Exception:
System.ArgumentOutOfRangeException: Index must be within the bounds of the List.
Parameter name: index
at 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: index
at 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

翻譯自: https://www.includehelp.com/dot-net/inserting-an-element-at-specified-index-in-a-collection-t-in-csharp.aspx

c#查找列表指定元素的索引

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542745.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542745.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542745.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

跨域技術(JSONP與CROS)

JSONP 我們發現&#xff0c;Web頁面上調用js文件時不受是否跨域的影響&#xff0c;凡是擁有"src"這個屬性的標簽都擁有跨域的能力&#xff0c;比如<script>、<img>、<iframe>。那就是說如果要跨域訪問數據&#xff0c;就服務端只能把數據放在js格式…

python3 array為什么不能放不同類型的數據_小白入門Python數據科學全教程lt;一gt;...

前言本文講解了從零開始學習Python數據科學的全過程&#xff0c;涵蓋各種工具和方法你將會學習到如何使用python做基本的數據分析你還可以了解機器學習算法的原理和使用說明先說一段題外話。我是一名數據科學家&#xff0c;在用SAS做分析超過5年后&#xff0c;我決定走出舒適區…

c winform 上傳文件到mysql_C# winform DevExpress上傳圖片到數據庫【轉】

實現功能如下圖&#xff1a;注明&#xff1a;此文使用的是DevExpress控件&#xff0c;winform 原生控件也是一樣使用方法。1.點擊選擇圖片按鈕&#xff0c;功能為通過對話框選擇要上傳的文件&#xff0c;并將該文件在下面的PictureEdit中顯示出來。具體代碼如下&#xff1a;pri…

V 8 nfs+drbd+heartbeat

V 8 nfsdrbdheartbeatnfsdrbdheartbeat&#xff0c;nfs或分布式存儲mfs只要有單點都可用此方案解決在企業實際生產場景中&#xff0c;nfs是中小企業最常用的存儲架構解決方案之一&#xff0c;該架構方案部署簡單、維護方便&#xff0c;只需通過配inotifyrsync簡單而高效的數據同…

nodemailer使用_如何使用Nodemailer使用HTML作為內容發送電子郵件 Node.js

nodemailer使用Prerequisite: 先決條件&#xff1a; How to send emails using Nodemailer | Node.js 如何使用Nodemailer發送電子郵件。 Node.js How to send emails with attachments using Nodemailer | Node.js 如何使用Nodemailer發送帶有附件的電子郵件。 Node.js This …

angularjs 元素重復指定次數_[LeetCode] 442. 數組中重復的數據

[LeetCode] 442. 數組中重復的數據題目鏈接&#xff1a; https://leetcode-cn.com/problems/find-all-duplicates-in-an-array難度&#xff1a;中等通過率&#xff1a;61.5%題目描述:給定一個整數數組 a&#xff0c;其中1 ≤ a[i] ≤ n &#xff08; n 為數組長度&#xff09;,…

docker 安裝mysql 實戰文檔_docker 安裝mysql

PassJava (佳必過) 項目全套學習教程連載中&#xff0c;關注公眾號第一時間獲取。docker 安裝mysql1.下載鏡像sudo docker pull mysql:5.7ubuntuVM-0-13-ubuntu:~$ sudo docker pull mysql:5.75.7: Pulling from library/mysqlc499e6d256d6: Pull complete22c4cdf4ea75: Pull c…

python 補前導零_Python正則表達式| 程序從IP地址中刪除前導零

python 補前導零Given an IP address as input, write a Python program to remove leading zeros from it. 給定一個IP地址作為輸入&#xff0c;編寫一個Python程序以從中刪除前導零。 Examples: 例子&#xff1a; Input: 216.08.094.196Output: 216.8.94.196Input: 216.08…

眼球追蹤

眼球追蹤類似于頭部追蹤&#xff0c;但是圖像的呈現取決于使用者眼睛所看的方向。例如&#xff0c;人們可以用“眼神”完成一種鐳射槍的瞄準。眼球追蹤技術很受VR專家們密切關注。Oculus創始人帕爾默拉奇就曾稱其為“VR的心臟”。對于人眼位置的檢測&#xff0c;能夠為當前所處…

mysql 創建分區表_Mysql分區表及自動創建分區Partition

Range分區表建表語句如下&#xff0c;其中分區鍵必須和id構成主鍵和唯一鍵CREATE TABLE test1 (id char(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘自增主鍵(guid)‘,create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘創建時間‘,partition_key …

python下載文件暫停恢復_Python關于Threading暫停恢復解決辦法

我們都知道python中可以是threading模塊實現多線程, 但是模塊并沒有提供暫停, 恢復和停止線程的方法, 一旦線程對象調用start方法后, 只能等到對應的方法函數運行完畢. 也就是說一旦start后, 線程就屬于失控狀態. 不過, 我們可以自己實現這些. 一般的方法就是循環地判斷一個標志…

信息系統狀態過程圖_過程狀態圖中使用的重要術語| 操作系統

信息系統狀態過程圖1)上下文切換 (1) Context Switching) Whenever a process is transferred within the system, it moves within different states. These states are known as the process states. When a process goes from one state to another state inside the system…

mysql 吧庫下的表名都加_mysql數據庫表名大小寫問題

mysql數據庫表名大小寫問題mysql數據庫linux版本表名、字段名默認大小寫敏感&#xff0c;即區分大小寫。查看mysql有關大小寫參數&#xff1a;lower_case_file_system是一個只讀參數&#xff0c;無法被修改&#xff0c;這個參數是用來告訴你在當前的系統平臺(linux\windows等)下…

rgb 灰色_金屬+RGB+無線,我要買爆這款海盜船VIRTUOSO鑒賞家游戲耳機

海盜船最近新出的旗艦耳機&#xff0c;VIRTUOSO RGB Wireless SE&#xff0c;中文名叫鑒賞家。耳機一改往日歐美電競風&#xff0c;改走金屬質感高大上簡約風&#xff0c;不過講真&#xff0c;這顏值我吃起來很香。考慮文章過長&#xff0c;我先概括一下入手理由&#xff0c;具…

python 基類 派生類_在Python中具有兩個子(派生)類的繼承示例

python 基類 派生類In this program, we have a parent class named Details and two child classes named Employee and Doctor, we are inheritance the class Details on the classes Employee and Doctor. And, finally creating two objects of Employee and Doctor class…

連接postgresql

# psycopg2enginecreate_engine(postgresqlpsycopg2://scott:tigerlocalhost/mydatabase)#python 連接postgresql使用psycopg2作為默認的DBAPIThe first time a method like Engine.execute()orEngine.connect()is called, the Engine establishes a real DBAPI connection to …

n的階乘程序python_Python程序對N階乘的尾隨零進行計數

n的階乘程序pythonFormula used: 使用的公式&#xff1a; Trailing 0s in N! Count of 5s in prime factors of n! floor(n/5) floor(n/25) floor(n/125) ....Example: 例&#xff1a; Input: N 23Output: 4Factorial of 23 is 25852016738884976640000 which has four …

c mysql使用場景_Mysql 場景

1個SQL題&#xff0c;1個場景題&#xff0c;會有點難度&#xff01;SQL題該SQL題大量涉及到row_number&#xff0c;case when&#xff0c;group by等高級用法&#xff0c;有一定的實用價值&#xff0c;總結出來&#xff0c;供日后參考Question.1&#xff1a;分組匯總給定篩選條…

以己為壑

2019獨角獸企業重金招聘Python工程師標準>>> 今天把軟件工程里面關于面向對象的設計學完了,使我對面向對象OOA和OOD的思想有了進一步的認識,各科知識千溝萬壑,犬牙交錯,的確是這樣,能蒙住自己眼的永遠是你自己,而不是這個世界,因為美就在那里;裹住自己雙足的的永遠是…

macos安裝vscode_如何使用VSCode進行PostgreSQL開發及調試

Visual Studio Code (VSCode)是一個輕量級但功能強大的源代碼編輯器&#xff0c;可在桌面上運行&#xff0c;適用于Windows&#xff0c;macOS和Linux。 它內置了對JavaScript&#xff0c;TypeScript和Node.js的支持&#xff0c;并具有豐富的其他語言(如C&#xff0c;C&#xff…