(1) Are array.binarysearch (...) Do you first sort the array to search? Answer: No code: use system;
Namespace arraysort {class item: iComparable {public int x; public int y; #REGION ICOMPARABLE member
Public item (int x, int y) {this.x = x; this.y = y;} public int compareto (object obj) {if (obj is int) returnix x.compareto ((int) obj); throw new Argumentexception ("Object is not a int!);
#ENDREGON} class class1 {[stathread] static void main (string [] args) {item [] myarray = new item [5]; myarray [0] = new item (5, 20); MyArray [1] = New Item (9, 6); MyArray [2] = New item (3, 6); MyArray [3] = New Item (28, 2); MyArray [4] = New Item (4, 12); int found = array. BinarySearch (MyArray, 3); for (int i = 0; i <5; i ) {system.console.writeline ("[{0}, {1}]", ((Item) MyArray [i]). X ((Item) MyArray [I]). Y);} if (i> = 0) System.Console.writeline ("[{0}, {1}]", ((iTeM) MyArray [Find]). X, (item) MyArray [Find]). Y);}}} Output: [5, 20] [9,6] [3,6] [28, 2] [4, 12] [3, 6]
(2): Implementing the implementation of Array.Sort, CompareTo () Different: use system; namespace arraysort {class item: iComparable {public int x; public int y; #REGION ICOMPARABLE member public item (int x, int y) {This.x = x; this.y = y;} public int compareto (Object obj) // parameter is an element {if (obj is item) Return X.Compareto ((item) OBJ) .x) Throw new argumentException ("Object is not a int!);
#ENDREGON} class class1 {[stathread] static void main (string [] args) {item [] myarray = new item [5]; myarray [0] = new item (5, 20); MyArray [1] = New Item (9, 6); MyArray [2] = new item (3, 6); MyArray [3] = new item (28, 2); MyArray [4] = New Item (4, 12); Array.Sort (MyArray ); For (INT i = 0; i <5; i ) {system.console.writeline ("[{0}, {1}]", ((item) MyArray [i]). X, ((item) MyArray [i]). Y);
}}} Output: [3,6] [4,12] [5,20] [9,6] [28, 2]