This C# program demonstrates how to find elements in a list.
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(""); // Check whether names are in the list Console.WriteLine("Contains Books:"); Console.WriteLine("---------------"); string[] saContains = new string[] { "John", "Isaiah", "Mark", "Titus", "Job", "Hebrews" }; foreach (string sName in saContains) { Console.WriteLine(sName + ((qBooksNT.Contains(sName)) ? " is " : " is not ") + "in the New Testament."); } Console.WriteLine(""); // Check whether an element with a certain property exists in the list Console.WriteLine("Exists:"); Console.WriteLine("-------"); for (int i = 0; i < 20; i += 3) { Console.WriteLine("There are " + (qBooksNT.Exists(x => x.Length == i) ? "" : "no ") + "elements with Length == " + i); } Console.WriteLine(""); // Find the first elements that contain the following strings Console.WriteLine("Find Books Containing:"); Console.WriteLine("----------------------"); string[] saFind = new string[] { "lat", "axe", "man", "orb", "ark", "lemon" }; foreach (string sSearch in saFind) { string sFirst = qBooksNT.Find(x => x.Contains(sSearch)); // if one was found if (sFirst != null) { Console.WriteLine(sFirst + " is the first NT book that contains \"" + sSearch + "\""); } else { Console.WriteLine( "There is no NT book that contains \"" + sSearch + "\""); } } Console.WriteLine(""); // Find all < "J" Console.WriteLine("Find All < \"J\" :"); Console.WriteLine("------------------"); List<string> qFoundBooks = qBooksNT.FindAll(delegate(string qBookName) { return (qBookName.CompareTo("J") < 0); }); foreach (string sBook in qFoundBooks) { Console.WriteLine(sBook); } Console.WriteLine(""); // Find index Console.WriteLine("Find Index:"); Console.WriteLine("-----------"); foreach (string sSearch in saFind) { int iIndex = qBooksNT.FindIndex(x => x.Contains(sSearch)); Console.WriteLine("The first NT book that contains \"" + sSearch + "\" is located at " + iIndex); } Console.WriteLine(""); // Index Of Console.WriteLine("Index Of:"); Console.WriteLine("---------"); foreach (string sName in saContains) { Console.WriteLine(sName + " is located at " + qBooksNT.IndexOf(sName)); } Console.WriteLine(""); } } }
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 Contains Books: --------------- John is in the New Testament. Isaiah is not in the New Testament. Mark is in the New Testament. Titus is in the New Testament. Job is not in the New Testament. Hebrews is in the New Testament. Exists: ------- There are no elements with Length == 0 There are no elements with Length == 3 There are elements with Length == 6 There are elements with Length == 9 There are no elements with Length == 12 There are elements with Length == 15 There are no elements with Length == 18 Find Books Containing: ---------------------- Galatians is the first NT book that contains "lat" There is no NT book that contains "axe" Romans is the first NT book that contains "man" There is no NT book that contains "orb" Mark is the first NT book that contains "ark" Philemon is the first NT book that contains "lemon" Find All < "J" : ------------------ Acts 1 Corinthians 2 Corinthians Galatians Ephesians Colossians 1 Thessalonians 2 Thessalonians 1 Timothy 2 Timothy Hebrews 1 Peter 2 Peter 1 John 2 John 3 John Find Index: ----------- The first NT book that contains "lat" is located at 8 The first NT book that contains "axe" is located at -1 The first NT book that contains "man" is located at 5 The first NT book that contains "orb" is located at -1 The first NT book that contains "ark" is located at 1 The first NT book that contains "lemon" is located at 17 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 . . .
© 20072024 XoaX.net LLC. All rights reserved.