Some operation methods for generic list and list

Source: Internet
Author: User
Tags ipoint

If you define a structure

1 Public   Struct Ipoint
2 {
3 Private Singlepoint _ C;
4 ///   <Summary>
5 /// Coordinates of the current vertex
6 ///   </Summary>
7 Public Singlepoint C
8 {
9 Get { Return _ C ;}
10 Set {_ C = Value ;}
11 }
12 Public Ipoint (singlepoint _ C)
13 {
14 This . _ C = _ C;
15 }
16 }
17 Public   Struct Singlepoint
18 {
19 Private   Int _ Y;
20 Private   Int _ X;
21 Public   Int Y
22 {
23 Get { Return _ Y ;}
24 Set {_ Y = Value ;}
25 }
26 Public   Int X
27 {
28 Get { Return _ X ;}
29 Set {_ X = Value ;}
30 }
31 Public Singlepoint ( Int _ Y, Int _ X)
32 {
33 This . _ Y = _ Y;
34 This . _ X = _ X;
35 }
36 }

However, if ipoint [] list = new ipoint [1] is required, the initial size of the array must be declared, which makes the actual operation more troublesome, it is not as flexible as arrays in JavaScript. At this time, you need to use the generic list <> for some processing, but there is a lot of inconvenience to the generic. In order to remember that some simple operations of the generic are recorded
These operations include:
1. List <>. Find ();
2. List <>. Sort ();
Start with find () and declare a list first.1 List<Ipoint>List= NewList<Ipoint>();

Here, public t find (predicate <t> match) is found during the "find" process. How can we change the predicate <t> match?
The following is an example: first, we need to create a finder. We only compare the coordinates of C in struct ipoint with those of others! 1 Public   Class Finder
2 {
3 Private Ipoint _ C;
4 ///   <Summary>
5 /// Coordinates of the current vertex
6 ///   </Summary>
7 Public Ipoint C
8 {
9 Get { Return _ C ;}
10 Set {_ C = Value ;}
11 }
12 Public Finder (ipoint cpoint)
13 {
14 This . _ C = Cpoint;
15 }
16 Public   Bool Findcurrentpoint (ipoint currpoint)
17 {
18 If (C. c.x = Currpoint. c.x && C. C. Y = Currpoint. C. Y)
19 {
20 Return   True ;
21 }
22 Else
23 {
24 Return   False ;
25 }
26 }
27 }

Start searching here: 1 Ipoint temppoint =   New Ipoint ( 1 , 2 );
2 Finder ifinder = New Finder (temppoint );
3 Ipoint findres = Closedlist. Find ( New Predicate < Ipoint > (Ifinder. findcurrentpoint ));

OK. Here we find the ipoint object of Y = 1, x = 2.
Sort ()
We need to first create a class that inherits from icomparer <>

  Public   Class Fcomparer: icomparer < Ipoint >
{
// Implement c.x ascending
Public   Int Compare (ipoint X, ipoint y)
{
Return(X. C. X. compareto (Y. c.x ));
}
}

The call is simpler.
Only

1 List. Sort ( New Fcomparer ());

That's all. I can't tell why I want to write it like this. Just remember how much it will be!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.