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

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#数组获取元素的索引

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380478.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux清理缓存cache,Linux清理cache缓存

当cache缓存占用太大&#xff0c;服务起不来&#xff0c;需要查看清理缓存查看缓存&#xff1a;free -m输入运行下面一行&#xff1a;echo 3 > /proc/sys/vm/drop_caches# 释放缓存区内存的方法1)清理pagecache(页面缓存)# echo 1 > /proc/sys/vm/drop_caches 或者 # sys…

c和汇编混合编程----shellcode----弹出计算器

先用c和汇编混合编程成功弹出计算器&#xff08;在VC里运行&#xff09; #include "stdio.h" #include "windows.h"int main(int argc, char* argv[]) {printf("begin\n");HINSTANCE libHandle;char *dll"kernel32.dll";libHandleLoa…

[导入]转:世界小姐形容他们国家male organ

世界小姐眼中的最后一问&#xff1a;形容一下他们国家male organMiss Universes last Question 环球小姐最后一题 Question: Ms America, how do you describe a male organ in your country? 问&#xff1a;美国小姐&#xff0c;请形容贵国男性的性器官。Ms America: Well, I…

scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

scala字符串替换Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string) Replacing a part of the string that matches our given regular expression can be done using multiple methods. 可以使用多种方法替换匹配给定正则表…

有趣的数

描述 把分数按下面的办法排成一个数表。 ① ② ⑥ ⑦ 1/1 1/2 1/3 1/4… ③ ⑤ ⑧ 2/1 2/2 2/3… ④ ⑨ 3/1 3/2 … ⑩ 4/1… … 我们以z字型方法给上表的每项编号。特定方法&#xff1a;第一项是1/1&#xff0c;然后是1/2、2/1、3/1、2/2、1/3、1/4、2/3……。编程输入项号N&…

linux系统的层次结构,关于Linux操作系统层次结构分析

本文转自http://www.jb51.net/LINUXjishu/214104.html首先来看一张图(这是Linux操作系统的大致层次结构)&#xff1a;最内层是硬件&#xff0c;最外层是用户常用的应用&#xff0c;比如说firefox浏览器&#xff0c;evolution查看邮件&#xff0c;一个计算流体模型等等。硬件是物…

Windows Vista版本比较

Windows Vista上市在即&#xff0c;对许多朋友来说目前急待解决的问题便是&#xff0c;在升级或购买时应该选择Windows Vista的哪个版本&#xff0c;哪个版本更契合自己的应用需求&#xff0c;究竟应该选择Windows Vista Home呢还是Windows Vista Ultimate?等等。 之前在Windo…

shellcode---c和汇编混合编程---弹出cmd

首先用C/C语言实现弹出cmd #include "stdio.h" #include "windows.h"int main(int argc, char* argv[]) {printf("begin\n");HINSTANCE libHandle;char *dll"kernel32.dll";libHandleLoadLibrary(dll);WinExec("cmd.exe",S…

ajax 复制到“剪贴板”

有时候可能会做一些“复制”按钮的功能&#xff0c;当用户点击“复制”按钮时&#xff0c;就会将要复制的内容复制出来&#xff0c;以下代码即实现“复制”按钮的功能。该功能需要用到AJAX的PageMethods来调用页面后台代码来实现。 调用PageMethods&#xff0c;需要引用 <sc…

找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串

找到最大回文子串Problem statement: 问题陈述&#xff1a; Given a string, you have to find the largest palindromic substring using O(1) space complexity. 给定一个字符串&#xff0c;您必须使用O(1)空间复杂度找到最大的回文子字符串。 Input:T Test caseT no of in…

日期计算

描述 如题&#xff0c;输入一个日期&#xff0c;格式如&#xff1a;2010 10 24 &#xff0c;判断这一天是这一年中的第几天。 输入 第一行输入一个数N&#xff08;0< N<100&#xff09;,表示有N组测试数据。后面的N行输入多组输入数据&#xff0c;每行的输入数据都是一个…

c语言程序设计编程解读,【答题】C语言程序设计问题与解释实验

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include#define N 13main(){int y,m,D,q,t0,i,day0,a0,Day,n,k,O[N]{0,31,29,31,30,31,30,31,31,30,31,30,31},p[N]{0,31,28,31,30,31,30,31,31,30,31,30,31};//y是年&#xff0c;m是月&#xff0c;D是日&#xff0c;q计算周几&am…

blog的转变

从技术为主的blog&#xff0c;变为记录生活琐事的blog&#xff0c;OMG&#xff01;天气晴朗&#xff0c;不过心情并不怎么轻松&#xff0c;论文下周必须要写完&#xff0c;同时留校结果也就该出来了。曾经一度以为&#xff0c;一个人最快乐自由。不过不得不承认&#xff0c;或许…

没有值的json字符串_Java中具有原始数据类型值的字符串连接

没有值的json字符串Given a string and some of the primitive data type values, we have to concatenate them with the string in Java. 给定一个字符串和一些原始数据类型值&#xff0c;我们必须将它们与Java中的字符串连接起来。 In the example below, we have declared…

a letter and a number(一封信和一个数字)

描述 we define f(A) 1, f(a) -1, f(B) 2, f(b) -2, … f(Z) 26, f(z) -26; Give you a letter x and a number y , you should output the result of yf(x). 我们定义f ( A ) 1&#xff0c;f ( A ) - 1&#xff0c;f ( B ) 2&#xff0c;f ( B ) - 2&#xff0c;…

c语言初始化字符数组为空,怎么把已经初始化的字符数组设置为空?

怎么把已经初始化的字符数组设置为空&#xff1f;两种方法&#xff0c;如下所示。方法一&#xff1a;代码演示#include #include int main(void){char a[]{x,y,z}; //定义字符数组&#xff0c;并初始化int i0;for(i0;i<3;i)printf("%c\t",a[i]); //输出初始化的数…

C和汇编混合编程---栈平衡

最近在搞C和汇编混合编程&#xff0c;对栈平衡有点小理解&#xff0c;记录一下 当我们调用一个API或者子程序时时&#xff0c;API和子程序可以理解为函数&#xff0c;我们不必在返回的时候平衡栈里面的函数参数&#xff0c;但C语言库函数要我们自己平衡栈数据&#xff0c; 比如…

[导入]2006年10大变态站名网站排名

作者&#xff1a; 马国良 | 2006年11月14日13时31分 | 【内容提要】第一名&#xff1a;妈妈说…… 入选原因&#xff1a;单看了两个域名就让人觉得变态“妈妈说就算你注册的域名再长google都能搜索出来”(mamashuojiusuannizhucedeyumingzaichanggoogledounengsousuochulai.cn)…

如何创建Java程序

(1),点击 (2),点击OK (3),File->New->Project… (4),Java Project->Next> (5),Project name随便填&#xff08;这里以qweqwe为例&#xff09; 然后Finish (6),鼠标右击qweqwe&#xff08;随便名称都可以&#xff09;->New->Class (7),同理&#xff0c;…

改变地址栏图标

<link rel"icon" href"/favicon.ico" type"image/x-icon" />转载于:https://www.cnblogs.com/heshuiping/archive/2011/06/04/2072515.html