c#数组获取元素的索引

Given a Collection<T> of integer types, and an index, we have to access the element from the given index.

给定一个整数类型的Collection <T>和一个索引,我们必须从给定索引访问元素。

To access an element of the Collection<T>, we use Collection<T>.Item[Int32 index] property.

要访问Collection <T>的元素,我们使用Collection <T> .Item [Int32 index]属性

Syntax:

句法:

    Collection<T>.Item[Int32 index];

Note: It may return exception (ArgumentOutOfRangeException), if index is either less than 0 or greater than the count.

注意:如果index小于0或大于count,则它可能返回异常( ArgumentOutOfRangeException )。

用C#代码访问Collection <T>的元素 (C# code to access an element of 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 elements from given index
Console.WriteLine("Element at index " + 0 + " is: " + iColl[0]);
Console.WriteLine("Element at index " + 1 + " is: " + iColl[1]);
Console.WriteLine("Element at index " + 2 + " is: " + iColl[2]);
Console.WriteLine("Element at index " + 3 + " is: " + iColl[3]);
}
}

Output

输出量

Total number of elements: 4
Element at index 0 is: 100
Element at index 1 is: 200
Element at index 2 is: 300
Element at index 3 is: 400

Displaying exception

显示异常

Here, we will access an element from -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 elements from given index
Console.WriteLine("Element at index " + 0 + " is: " + iColl[0]);
Console.WriteLine("Element at index " + 1 + " is: " + iColl[1]);
Console.WriteLine("Element at index " + 2 + " is: " + iColl[2]);
Console.WriteLine("Element at index " + 3 + " is: " + iColl[3]);
// displaying element from index "-1"
Console.WriteLine("Element at index " + -1 + " is: " + iColl[-1]);
}
}

Output

输出量

Total number of elements: 4
Element at index 0 is: 100
Element at index 1 is: 200
Element at index 2 is: 300
Element at index 3 is: 400
Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException ()
[0x0000c] in <65984520577646ec9044386ec4a7b3dd>:0
at System.Collections.Generic.List`1[T].get_Item
(System.Int32 index) [0x00009] in <65984520577646ec9044386ec4a7b3dd>:0
at System.Collections.ObjectModel.Collection`1[T].get_Item (System.Int32 index)
[0x00000] in <65984520577646ec9044386ec4a7b3dd>:0
at IncludeHelp.Main () [0x00129] in <775a4ba6f9ff4ee287095185056138d8>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException ()
[0x0000c] in <65984520577646ec9044386ec4a7b3dd>:0
at System.Collections.Generic.List`1[T].get_Item (System.Int32 index)
[0x00009] in <65984520577646ec9044386ec4a7b3dd>:0
at System.Collections.ObjectModel.Collection`1[T].get_Item
(System.Int32 index) [0x00000] in <65984520577646ec9044386ec4a7b3dd>:0
at IncludeHelp.Main () [0x00129] in <775a4ba6f9ff4ee287095185056138d8>:0

翻译自: https://www.includehelp.com/dot-net/getting-an-element-of-collection-t-from-specified-index-in-csharp.aspx

c#数组获取元素的索引

c#数组获取元素的索引_获取元素集合 从C#中的指定索引相关推荐

  1. 索引视图是否物理存储在数据库中以及使用索引视图的一些见解

    索引视图是否物理存储在数据库中以及使用索引视图的一些见解 前言 这个话题我本来是写在文章里没有写在随笔里的,不过赶脚不写在随笔里其他人就看不到了,因为小弟对视图的认识不深 希望写在随笔里让大家也讨论一 ...

  2. python 一维数组所有元素是否大于_利用Python进行数据分析(5) NumPy基础: ndarray索引和切片...

    概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...

  3. java for 获取索引_获取Java列表中的对象索引

    我的(Android)Java程序中有一个字符串列表,我需要获取列表中对象的索引.问题是,我只能找到有关如何查找对象的第一个和最后一个索引的文档.如果我的列表中有3个或更多相同的对象怎么办?我怎样才能 ...

  4. 【python】list 删除列表中某个元素的3种方法;附加删除numpy数组中的指定索引元素的方法

    方法 python中关于删除list中的某个元素,一般有三种方法: remove.pop.del 实例 1.remove: 删除单个元素,删除首个符合条件的元素,按值删除 2.pop: 删除单个或多个 ...

  5. mysql 字典索引_【大白话mysql】你真的了解 mysql 索引吗?

    本文来源于公众号: 跬步匠心 什么是索引? 当我们使用汉语字典查找某个字时,我们会先通过拼音目录查到那个字所在的页码,然后直接翻到字典的那一页,找到我们要查的字,通过拼音目录查找比我们拿起字典从头一页 ...

  6. lucene索引_在崩溃或断电后测试Lucene的索引耐久性

    lucene索引 Lucene有用的事务功能之一是索引持久性 ,它可以确保一旦成功调用IndexWriter.commit ,即使操作系统或JVM崩溃或断电,或者您杀死-KILL JVM进程,重启后索 ...

  7. html使div内部元素水平排列_实现元素水平排列的六种方法

    众所周知,块级元素默认是垂直排列的,行内元素是水平排列的,而在布局时基本上都是用块级元素,如div等常用块级标签,那么如何让块级元素也进行水平排列呢?这篇文章给大家介绍六中方式,实现块级元素的水平排列 ...

  8. mysql join on 索引_连接查询,表关联查询join on,索引,触发器,视图

    一.连接查询 1.统计每一个部门的人数  "部门名,部门的人数" select department,count(eid) from employee group by depar ...

  9. mysql 大量数据 更改索引_一文看懂ICP原理--MySQL用索引去表里取数据的一种优化...

    概述 今天主要介绍一下mysql的ICP特性,可能很多人都没听过,这里用一个实验来帮助大家加深一下理解. 一.Index_Condition_Pushdown Index Condition Push ...

最新文章

  1. linux apache配置多线程,linux apache 日志配置
  2. HUST 1586 数字排列
  3. 拥有特殊闭合标签的标签
  4. 开发日记-20190606 关键词 闲散度日
  5. python开发_python中的range()函数
  6. 伺服步进电机选型软件_关于伺服步进电机的28个问题
  7. UVA 10603 - Fill(dijkstra + 状态图)
  8. 本地连接和音量图标显示
  9. 智能家居简单实现---使用ESP8266简单实现和APP通讯
  10. Introduction to CALayers Tutorial
  11. appinventor如何做个游戏_单亮:游戏的重要性
  12. noapic和acpi=off能帮我做到什么?
  13. 有趣的数学结论(未完待续)
  14. 获取手机唯一标识插件_H5能获取到手机设备ID或者手机浏览器唯一识别码吗
  15. 帆软设计器连接远程服务器,帆软设计器远程连接数据库问题
  16. crh寄存器_STM32的寄存器控制SDA_IN()/SDA_OUT()
  17. 2021CCPC桂林游记
  18. zeebe入门课程10-bpmn元素的支持7(exclusive gateway )
  19. 高管离职、亏损百亿、合规难题,首汽约车的努力配不上野心
  20. 数学问题:导函数的左右极限与函数的左右导数是一回事吗?

热门文章

  1. linux iso合并,把RedHat Linux 5.0的CD ISO合并成DVD的脚本
  2. Vue 封装的组件生命周期钩子
  3. vue-cli使用swiper4在ie以及safari报错
  4. jquery中点击切换的实现
  5. 认识Skeleton Screen【屏幕加载骨架】
  6. 关于VUE项目地图开发中大量点标记绘制一些总结
  7. 中国移动MM7 API用户手册(七)
  8. echarts折线图相关
  9. 【BZOJ 1098】办公楼(补图连通块个数,Bfs)
  10. Linux常用开发环境软件-redis安装