查找列表中元素索引的 C# 程序
设置一个列表并添加元素 −
List<int> val = new List<int>(); // integer elements val.Add(35); val.Add(55); val.Add(68);
假设现在我们需要找到元素 68 的索引。为此,请使用 IndexOf() 方法 −
int index = val.IndexOf(68);
以下是完整的代码 −
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> val = new List<int>();
// integer elements
val.Add(35);
val.Add(55);
val.Add(68);
// finding the index of element 68
int index = val.IndexOf(68);
Console.WriteLine(index);
}
}输出
2
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP