Generics C#

Finding the Last Elements in a List

This C# program demonstrates how to find the last elements in a list.

Program.cs

using System;
using System.Collections.Generic;


namespace XoaX {

    class Program {
        static void Main(string[] args) {
            // An array of the names of new testament books
            string[] saNewTestamentBooks = new string[]{
                "Matthew", "Mark", "Luke", "John", "Acts", "Romans",
                "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians",
                "Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians",
                "1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews",
                "James", "1 Peter", "2 Peter", "1 John", "2 John", "3 John",
                "Jude", "Revelation"
            };

            List<string> qBooksNT = new List<string>(saNewTestamentBooks);

            // Display the list of books
            Console.WriteLine("Books of the New Testament:");
            Console.WriteLine("---------------------------");
            foreach (string sBook in qBooksNT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");

            // Find the first elements that contain the following strings
            Console.WriteLine("Find Last Books Containing:");
            Console.WriteLine("--------------------------");
            string[] saFind = new string[] {
                "lat", "axe", "an", "orb", "ar", "on"
            };
            foreach (string sSearch in saFind) {
                string sFirst = qBooksNT.FindLast(x => x.Contains(sSearch));
                // if one was found
                if (sFirst != null) {
                    Console.WriteLine(sFirst +
                        " is the last NT book that contains \"" + sSearch + "\""); 
                } else {
                    Console.WriteLine(
                        "There is no NT book that contains \"" + sSearch + "\""); 
                }
            }
            Console.WriteLine("");

            // Find last index
            Console.WriteLine("Find Last Index:");
            Console.WriteLine("----------------");
            foreach (string sSearch in saFind) {
                int iIndex = qBooksNT.FindLastIndex(x => x.Contains(sSearch));
                Console.WriteLine("The last NT book that contains \"" + sSearch +
                    "\" is located at " + iIndex);
            }
            Console.WriteLine("");

            // Last Index Of
            Console.WriteLine("Last Index Of:");
            Console.WriteLine("--------------");
            string[] saContains = new string[] { 
                "John", "Isaiah", "Mark", "Titus", "Job", "Hebrews" 
            };
            foreach (string sName in saContains) {
                Console.WriteLine(sName + " is located at " + qBooksNT.LastIndexOf(sName));
            }
            Console.WriteLine("");
        }
    }
}
 

Output

Books of the New Testament:
---------------------------
Matthew
Mark
Luke
John
Acts
Romans
1 Corinthians
2 Corinthians
Galatians
Ephesians
Philippians
Colossians
1 Thessalonians
2 Thessalonians
1 Timothy
2 Timothy
Titus
Philemon
Hebrews
James
1 Peter
2 Peter
1 John
2 John
3 John
Jude
Revelation

Find Last Books Containing:
--------------------------
Revelation is the last NT book that contains "lat"
There is no NT book that contains "axe"
2 Thessalonians is the last NT book that contains "an"
There is no NT book that contains "orb"
Mark is the last NT book that contains "ar"
Revelation is the last NT book that contains "on"

Find Last Index:
----------------
The last NT book that contains "lat" is located at 26
The last NT book that contains "axe" is located at -1
The last NT book that contains "an" is located at 13
The last NT book that contains "orb" is located at -1
The last NT book that contains "ar" is located at 1
The last NT book that contains "on" is located at 26

Last Index Of:
--------------
John is located at 3
Isaiah is located at -1
Mark is located at 1
Titus is located at 16
Job is located at -1
Hebrews is located at 18

Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.