Podobne
- Strona startowa
- Chmielewska Joanna Zlota mucha
- Edwards Eve Alchemia miłoÂści 03 Gra o miłoÂść. Kroniki rodu Lacey
- Dick Philip K Kosmiczne Marionetki (4)
- Pamietnik Z Konferencji Zydozna
- Demostenes Mowy wybrane
- Balzac H. Ojciec Goriot
- Morrell Dav
- Ondaatje Michael Angielski pacjent
- Lem Stanislaw Swiat na krawedzi (SCAN dal 933 (2)
- Target Organ Toxicity in Marine and Freshwater Teleosts. Vol. 1
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- starereklamy.pev.pl
Cytat
Do celu tam się wysiada. Lec Stanisław Jerzy (pierw. de Tusch-Letz, 1909-1966)
A bogowie grają w kości i nie pytają wcale czy chcesz przyłączyć się do gry (. . . ) Bogowie kpią sobie z twojego poukładanego życia (. . . ) nie przejmują się zbytnio ani naszymi planami na przyszłość ani oczekiwaniami. Gdzieś we wszechświecie rzucają kości i przypadkiem wypada twoja kolej. I odtąd zwyciężyć lub przegrać - to tylko kwestia szczęścia. Borys Pasternak
Idąc po kurzych jajach nie podskakuj. Przysłowie szkockie
I Herkules nie poradzi przeciwko wielu.
Dialog półinteligentów równa się monologowi ćwierćinteligenta. Stanisław Jerzy Lec (pierw. de Tusch - Letz, 1909-1966)
[ Pobierz całość w formacie PDF ]
.The main characteristic of these two collections is how you add and remove items tothem.When you add items to a Queue, the items are appended to the collection.When you removeitems, they re removed from the top of the collection.You d use this collection to emulate the cus-tomer line in a bank or a production line.The Stack collection inserts new items at the top, and you can only remove the top item.TheStack collection is a FIFO (first in first out) structure, while the Queue class is a LIFO structure(last in first out).You d use this collection to emulate the stack maintained by the CPU, one of themost crucial structures for the operating system and applications alike.Stacks and Queues are usedheavily in computer science, but they aren t as common in business applications.I m not going todiscuss any more collections in this book, but you can look them up in the documentation.Thereare quite a few more interesting topics to cover in this chapter and most important is how to save acollection to a disk file and read it back.The IEnumerator and IComparer InterfacesJudging by its title, you probably thought this is a section for C++ programmers adapted for VB pro-grammers.IEnumerator and IComparer are two objects that unlock some of the most powerful fea-tures of collections.The proper term for IEnumerator and IComparer is interface, a term I will describeshortly.If you don t want to get too technical about interfaces, think of them as objects.The IEnu-merator object retrieves a list of pointers for all the items in a collection, and you can use it to iteratethrough the items in a collection.Every collection has a built-in enumerator, and you can retrieve it bycalling its GetEnumerator method.The IComparer object exposes the Compare and CompareTomethods, which tells the compiler how to compare two objects of the same type.Once the compilerknows how to compare the objects, it can sort a collection of objects with the same type.The IComparer interface consists of a function that compares two items and returns a value indi-cating their order (which one is the smaller item, or whether they re equal).The Framework can t com-pare objects of all types.It only knows how to compare the base types integers, strings, and so on.Itdoesn t know how to compare two rectangles, or two color objects.If you have a collection of col-ors, you may want to sort them according to their luminance, saturation, brightness, and so on.Thecompiler can t make any assumptions as to how you may wish to sort your collection, and, of course,it doesn t expose members to sort a collection in all possible ways.Instead, it gives you the option tospecify a function that compares two colors (or two objects of any other type, for that matter) anduses this function to sort the collection.The same function is used by the BinarySearch method, towww.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c11.qxd 11/11/01 4:18 PM Page 509THE IENUMERATOR AND ICOMPARER INTERFACES 509locate an item in a sorted collection.In effect, the IComparer interface is a function that knows howto compare two Color objects, for our example.If the collection contains items of a custom Struc-ture, the IComparer interface is a function that knows how to compare two instances of the customStructure.So, what is an interface? An interface is another term in object-oriented programming and describesa very simple technique.When we write the code for a class, we may not know how to implement afew operations, but we do know that they ll have to be implemented later.We insert a placeholderfor these operations (a function declaration) and expect that the application that uses the class willprovide the actual implementation of these functions.All collections expose a Sort method, whichsorts the items in the collection by comparing them to one another.To do so, the Sort method callsa function that compares two items and returns a value indicating their relative order.Any class thatexposes a function that can compare its objects can be sorted.The Integer data type, which is imple-mented by the System.Integer class, exposes such a function, and so do all the base types.Customobjects must provide their own comparison function or more than a single function, if you want tosort them in multiple ways.Since you can t edit the collection s Sort method s code, you must supplyyour comparison function through a mechanism that the class can understand.This is what theIComparer interface is all about.The code that compares two objects of the same type is actuallytrivial
[ Pobierz całość w formacie PDF ]