Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

조건과 일치하는 요소를 검색하고 C#의 전체 목록 내에서 마지막으로 발생하는 0부터 시작하는 인덱스를 반환합니다.

<시간/>

조건과 일치하는 요소를 검색하고 전체 목록 내에서 마지막으로 나타나는 0부터 시작하는 인덱스를 반환하려면 코드는 다음과 같습니다. -

using System;
using System.Collections.Generic;
public class Demo {
   private static bool demo(int i) {
      return ((i % 10) == 0);
   }
   public static void Main(String[] args) {
      List<int> list = new List<int>();
      list.Add(200);
      list.Add(215);
      list.Add(310);
      list.Add(500);
      list.Add(600);
      Console.WriteLine("List elements...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }
      Console.WriteLine("Last Index of the satisfying condition = "+list.FindLastIndex(demo));
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

List elements...
200
215
310
500
600
Last Index of the satisfying condition = 4

이제 다른 예를 살펴보겠습니다 -

using System;
using System.Collections.Generic;
public class Demo {
   private static bool demo(int i) {
      return ((i % 2) == 0);
   }
   public static void Main(String[] args) {
      List<int> list = new List<int>();
      list.Add(200);
      list.Add(215);
      list.Add(310);
      list.Add(500);
      list.Add(655);
      Console.WriteLine("List elements...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }
      Console.WriteLine("Last Index of the satisfying condition = "+list.FindLastIndex(demo));
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

List elements...
200
215
310
500
655
Last Index of the satisfying condition  = 3