barcode printer vb.net Finding List Items in C#

Printer Data Matrix 2d barcode in C# Finding List Items

Finding List Items
Create Data Matrix In Visual C#
Using Barcode drawer for VS .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
DataMatrix Recognizer In Visual C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
The List<T> class has a number of methods that help you find data in the collection. I tend not to use these methods, preferring to use LINQ to Objects, which you can learn more about in 27. Nonetheless, these methods can be useful and, because they are tailored to the implementation of the List<T> class, offer a high-performance alternative to LINQ. The methods are summarized in Table 19-6. Table 19-6. Members for Finding List Items
Make Barcode In C#.NET
Using Barcode creation for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Create Code 39 Full ASCII In Visual C#.NET
Using Barcode creator for .NET Control to generate, create USS Code 39 image in Visual Studio .NET applications.
www.OnBarcode.com
Member
EAN-13 Drawer In C#.NET
Using Barcode maker for .NET Control to generate, create GS1 - 13 image in .NET framework applications.
www.OnBarcode.com
Paint ECC200 In Visual C#
Using Barcode printer for .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
BinarySearch(T) BinarySearch(T, IComparer(T)) BinarySearch(int, int, T, IComparer(T))
Encoding PDF417 In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
ANSI/AIM I-2/5 Maker In Visual C#.NET
Using Barcode creator for .NET Control to generate, create USS ITF 2/5 image in Visual Studio .NET applications.
www.OnBarcode.com
Description
Encoding ECC200 In Java
Using Barcode generator for Android Control to generate, create Data Matrix 2d barcode image in Android applications.
www.OnBarcode.com
Painting Data Matrix In C#.NET
Using Barcode printer for .NET Control to generate, create Data Matrix image in VS .NET applications.
www.OnBarcode.com
Uses a binary search algorithm to return the index of a specified item in the list
Printing EAN13 In Objective-C
Using Barcode printer for iPhone Control to generate, create EAN13 image in iPhone applications.
www.OnBarcode.com
Drawing 2D In .NET
Using Barcode printer for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
CHAPTER 19 COLLECTIONS
Code 39 Extended Creator In .NET
Using Barcode generator for ASP.NET Control to generate, create Code39 image in ASP.NET applications.
www.OnBarcode.com
Creating Barcode In None
Using Barcode maker for Word Control to generate, create Barcode image in Office Word applications.
www.OnBarcode.com
Member
Drawing Data Matrix In Java
Using Barcode generation for Java Control to generate, create Data Matrix image in Java applications.
www.OnBarcode.com
UPC-A Supplement 5 Printer In Java
Using Barcode drawer for Android Control to generate, create Universal Product Code version A image in Android applications.
www.OnBarcode.com
Exists(Predicate(<T>)
Barcode Recognizer In Java
Using Barcode Control SDK for BIRT reports Control to generate, create, read, scan barcode image in BIRT applications.
www.OnBarcode.com
ECC200 Reader In Visual Basic .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Description
Drawing ANSI/AIM Code 128 In None
Using Barcode generation for Office Word Control to generate, create Code 128 Code Set B image in Word applications.
www.OnBarcode.com
Generating Code 39 Full ASCII In None
Using Barcode encoder for Font Control to generate, create Code 39 image in Font applications.
www.OnBarcode.com
Returns true if there is at least one element in the list for which the predicate returns true Returns the first item in the list for which the predicate returns true Returns a List<T> containing all the items in the list for which the specified predicate returns true Returns the index of the first element in the list for which the predicate returns true
Find(Predicate<T>)
FindAll(Predicate<T>)
FindIndex(Predicate<T>)) FindIndex(int, Predicate<T>) FindIndex(int, int, Predicate<T>) FindLast(Predicate<T>)
Returns the last item in the list for which the predicate returns true Returns the index of the last item in the list for which the predicate returns true
FindLastIndex(Predicate<T>) FindLastIndex(int, Predicate<T>) FindLastIndex(int, int, Predicate<T>) LastIndexOf(T) LastIndexOf(T, int) LastIndexOf(T, int, int)
Returns the index of the last occurrence of a given item in the list
Finding Items Using Binary Searches
Using the BinarySearch method is a very efficient way of finding an element in the list, but it requires that the elements in the collection are in order. You can either insert the elements in order as you create or build the collection, or you can collect the items in any order and then use the Sort method, which I describe in the following section. Listing 19-4 shows the BinarySearch method in action. Listing 19-4. Using the BinarySearch Method using System; using System.Collections.Generic; namespace Listing 04 { class Listing 04 { static void Main(string[] args) {
CHAPTER 19 COLLECTIONS
List<string> list = new List<string>() { "apple", "apricot", "banana", "cherry", "mango", "orange", "pear"}; int index1 = list.BinarySearch("orange"); Console.WriteLine("Index 1: {0}", index1); int index2 = list.BinarySearch("papaya"); Console.WriteLine("Index 2 : {0} {1}", index2, ~index2); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } } The BinarySearch method depends on the IComparer<T> interface, which I describe later in this chapter. The BinarySearch overload that takes one argument uses the default IComparer<T>, while the other two overloads let you specify the comparer to use. Compiling and running the code in Listing 19-4 gives the following results: Index 1: 5 Index 2 : -7 6 Press enter to finish If the value you are looking for is in the collection, the BinarySearch method returns the index it occupies in the list. You can see this when I search for orange, which gives me a result of 5, indicating that orange is the sixth element in the list (the index is zero-based). If the value you are looking for is not in the collection, then the result will be negative. If you NOT the result with the NOT operator (~), you will get the index of the first element in the list, which is greater than the item you were looking for. You can use this value to insert the item you searched for and preserve the ordering of the list. In the listing, I search for papaya, which is not in the collection. The result I receive is negative (-7), and the NOT result of this value is 6, meaning that if I call this:
list.Insert(6, "papaya");
the order of the list will be preserved, and I can conduct further binary searches without having to sort the data. The BinarySearch method will return unpredictable results if you use it on unsorted data. The data in the example is sorted alphabetically, but you can search data that has been sorted using a different approach by providing an implementation of the IComparer<T> interface as an argument to the BinarySearch method. I show you how to sort and search using this interface in the Sorting List Items section later in the chapter.
Copyright © OnBarcode.com . All rights reserved.