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#查找列表指定元素的索引

c#查找列表指定元素的索引_在集合的指定索引处插入元素 在C#中相关推荐

  1. mysql四种常用的索引_四种常见的索引类型

    主键索引:数据记录里面不能有 null,数据内容不能重复,在一张表里面不能有 多个主键索引. 普通索引:使用字段关键字建立的索引,主要是提高查询速度 唯一索引:字段数据是唯一的,数据内容里面能否为 n ...

  2. 创建二级索引_技术分享 | InnoDB 排序索引的构建

    原创: 管长龙 译 爱可生开源社区 3天前 作者:Satya Bodapati 从 MySQL 5.7 开始,开发人员改变了 InnoDB 构建二级索引的方式,采用自下而上的方法,而不是早期版本中自上 ...

  3. 一维数组所有元素是否大于_【C语言】- 指向一维数组元素的指针!你今天学习了吗?...

    前面我们已经学习了指针,如果指针存储了某个变量的地址,我们就可以说指针指向这个变量.数组及其数组元素都占有存储空间,都有自己的地址,因此指针变量可以指向整个数组,也可以指向数组元素. 一.用指针指向一 ...

  4. mysql select不走索引_避免写出不走索引的SQL, MySQL

    在MySQL中,并不是你建立了索引,并且你在SQL中使用到了该列,MySQL就肯定会使用到那些索引的,有一些情况很可能在你不知不觉中,你就"成功的避开了"MySQL的所有索引. 现 ...

  5. 怎样合理创建es索引_如何通过Elasticsearch创建索引库?

    今天是刘小爱自学Java的第158天. 感谢你的观看,谢谢你. 学习计划安排如下:Elasticsearch作为一门全文检索技术,那它是如何使用的呢? 先学习Elasticsearch的一些语法,后续 ...

  6. 行存储索引改换成列存储索引_如何使用列存储索引来改善数据仓库登台环境

    行存储索引改换成列存储索引 My team and I were recently tasked with refactoring older data marts, particularly tho ...

  7. 数据库mysql建立索引_为mysql数据库建立索引

    前些时候,一位颇高级的程序员居然问我什么叫做索引,令我感到十分的惊奇,我想这绝不会是沧海一粟,因为有成千上万的开发者(可能大部分是使用MySQL的)都没有受过有关数据库的正规培训,尽管他们都为客户做过 ...

  8. mysql为什么添加索引_当我添加新索引时,为什么MySQL中索引的基数保持不变?

    如果表中只有1行,则索引的基数当然应为1.它只是计算唯一值的数量. 如果您将索引视为基于存储桶的查找表(如散列),则基数是存储桶的数量. 以下是它的工作原理:当您在一组列(a,b,c,d)上构建索引时 ...

  9. mysql查询是否走索引_探索MySQL是否走索引(一)——范围查询一定走索引吗?

    首先,准备4个版本的数据库,5.5/5.6/5.7/8.0 然后,每个库中有一模一样的表,数据量一样,建立的索引一样,InnoDB引擎. 先看一看,各个版本中,索引统计信息(Cardinality代表 ...

最新文章

  1. Hadoop系列之一:hadoop部署安装
  2. Apache Tomcat 拒绝服务漏洞通告
  3. mybatis 中#和$的区别
  4. 利用Mycat中间件实现RDS MySQL的分库分表及读写分离功能
  5. 一周要回审9000万条视频,内容审核师能被AI拯救吗?
  6. android 设计qq界面,[考试求助]仿照腾讯QQ应用软件,编写Android程序,登录界面设计...
  7. Make Membership header extension fields editable
  8. ConcurrentHashMap能完全替代HashTable吗?
  9. 三维点云网络PointNet——模型及代码分析
  10. PHPCMSV9前台会员注册提示操作失败的解决办法
  11. 华为P20云文档空间满了怎么清理_原来华为手机能这样清理垃圾,怪不得别人的手机再用两年不卡顿...
  12. 基于visual Studio2013解决面试题之0507字符串转整数
  13. android中自适应布局教程,Android实现自适应正方形GridView
  14. windows资源监视器中内存项(提交、工作集、可共享、专用)的含义及区别
  15. 从softmax到ArcFace
  16. Cradle 从工程学角度预测血管和气管的流量并研究生物
  17. rft测试软件,RFT 测试框架的核心
  18. MATLAB绘制小胖墩
  19. Python常见问题解决办法汇总
  20. 同花顺校园招聘笔试题 2014 武汉站

热门文章

  1. 店铺咨询系统c语言,课内资源
  2. C++面试常见问题一
  3. 树莓派摄像头 C++ OpenCV YoloV3 实现实时目标检测
  4. oracle p l,使用P.A.L制作便携软件 (一) 基本原理 | 么么哒拥有者
  5. teleport 组件的作用_人脸识别综述! 覆盖人脸检测,预处理和特征表示三大核心组件!...
  6. orange pi java_[中文]Orange Pi家族各大成员一览表
  7. linux基线核查脚本
  8. 【Docker】容器镜像有哪些特性
  9. linux产生随机数方法
  10. MySQL:查询条件