1. Find objects
Copy Code code as follows:
person P1 = new Person ("http://www.my400800.cn", 18);
person P2 = new Person ("http://www.my400800.cn", 19);
Person P3 = new Person ("http://www.my400800.cn", 20);
person[] Persons = ... {p1, p2, p3};
Find the position in the array where P2 is located
Array.indexof < person > (persons, p2);
2. Find values
Copy Code code as follows:
person P1 = new Person ("http://www.my400800.cn", 18);
person P2 = new Person ("http://blog.my400800.cn", 19);
Person P3 = new Person ("http://blog.my400800.cn/400 telephone", 20);
person[] Persons = ... {p1, p2, p3};
person P4 = new Person (p2. Name, p2. Age);
Find the location of the same element in the array as P4
Array.indexof < person > (persons, p4);
However, this method must enable the person to overload the Equals comparison method of the object
Copy Code code as follows:
public override bool Equals (object obj)
... {
person is = obj as person;
if (person = = null) return false;
Return (this. Name = = Person.name && this. Age = = Person.age);
}
The second method to find by object value
Implement Icomparabler interface
Copy Code code as follows:
public int CompareTo (object obj)
... {
person is = obj as person;
if (person = = null)
throw new Exception ("the method or operation isn't implemented.");
Start with the age comparison
int ageresult = this. Age.compareto (person.age);
if (Ageresult = 0)
... {
If the age equals in the sitting name comparison
return this. Name.compareto (Person.name);
}
Else
... {
return ageresult;
}
}
After implementing the IComparable interface, you can use Array.BinarySearch () to find the
Copy Code code as follows:
Get the subscript with the same value for person in persons
If multiple values are the same, BinarySearch will take the last
An array subscript with the same value
Array.BinarySearch < person > (persons, Person);
Note: You must manipulate a sorted array using Array.binaryseach
3. Sorting
As long as the object implements the IComparable interface, you can sort by using the static method sort in the array
Copy Code code as follows:
Must enable the object of comparison to implement the IComparable interface
Array.Sort < person > (persons);