C# 程序用于在数组中查找最后一个匹配的元素
要找到最后一个匹配元素,请使用 Array.LastIndexOf 方法。如果元素不存在于整数数组中,则返回 -1。
以下是数组 −
int[] val = { 97, 45, 76, 21, 89, 45 };现在,让我们说你需要搜索元素 45 的最后一个索引。为此,请使用 Array.LastIndexOf() 方法 −
int res = Array.LastIndexOf(val, 45);
以下是一个示例 −
示例
using System;
using System.Text;
public class Demo {
public static void Main() {
int[] val = { 97, 45, 76, 21, 89, 45 };
// last Index of element 45
int res = Array.LastIndexOf(val, 45);
// Display the index
Console.WriteLine("Index of element 45 is = "+res);
}
}输出
Index of element 45 is = 5
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP