This exampleProgramThe function is to find the maximum and minimum values in the array.
First, define a generic structure pair to save this value:
Public StructPair <t>
{
PublicT max;
PublicT min;
}
Next, design a generic maxmin class, and provide a public getmaxminvaulefromarray method to encapsulate "find the maximum and minimum values in the array ".Algorithm.
Public Class Maxmin <t> Where T: icomparable <t>
{
// Process data and obtain the maximum and minimum values
Public Static Pair <t> getmaxminvaulefromarray (T [] ARR)
{
Pair <t> ret;
Ret. max = arr [ 0 ];
Ret. min = arr [ 0 ];
For ( Int I = 1 ; I <= arr. getupperbound ( 0 ); I ++)
{
If (Ret. Max. compareto (ARR [I]) <0 )
Ret. max = arr [I];
If (Ret. Min. compareto (ARR [I])> 0 )
Ret. min = arr [I];
}
Return RET;
}
}
To compare the elements of a generic array, the type parameter T is required to implement the icomparable <t> generic interface.
The int and Char Types in C # both implement the icomparable <t> generic interface, so it can be used directly. However, the complex complecnum class is written by ourselves and must be written.CodeEnable the icomparblae <complexnum> interface.
Class Complexnum: icomparable <complexnum>
{
Public Double A; // Real-time
Public Double B; // Virtual part
Public Complexnum ( Double Avalue, Double Bvalue)
{
A = avalue;
B = bvalue;
}
// Returns a string in the form of a + bi according to the plural value.
Public Override String tostring ()
{
Return (A. tostring () + " + " + B. tostring () + " I " );
}
// Evaluate the modulo of the plural number
Public Double Getmod ()
{
Return Math. SQRT (A * A + B * B );
}
// Compare the sizes of two plural numbers by modulo
Int Icomparable <complexnum>. compareto (complexnum other)
{
If (Math. Abs (getmod ()-Other. getmod () < 0.000001 )
Return 0 ;
If (Getmod ()> other. getmod ())
Return 1 ;
Else
Return - 1 ;
}
}
Now the complexnum object can be compared to the size. The following sample code finds the largest and smallest plural numbers in the plural array.
Public Partial Class Frmgpexample: Form
{
Public Frmgpexample ()
{
Initializecomponent ();
}
Private Char [] Chararray = New Char [ 10 ];
Private Int [] Intarray = New Int [ 10 ];
Private Complexnum [] complexarray = New Complexnum [ 10 ];
Private Void Onbuttonclick ()
{
// Fill Array
Fillarray ();
If (Rdointeger. Checked)
Showresult < Int > (Maxmin < Int >. Getmaxminvaulefromarray (intarray ));
If (Rdochar. Checked)
Showresult < Char > (Maxmin < Char >. Getmaxminvaulefromarray (chararray ));
If (Rdocomplex. Checked)
Showresult <complexnum> (maxmin <complexnum>. getmaxminvaulefromarray (complexarray ));
}
Private Void Showresult <t> (pair <t> RET)
{
Lblmax. Text = " Maximum value: " + Ret. Max. tostring ();
Lblmin. Text = " Minimum value: " + Ret. Min. tostring ();
}
// Fill the array with random data and display it in the list box
Private Void Fillarray ()
{
Int I;
Random ran = New Random ();
Lstdata. Items. Clear ();
If (Rdointeger. Checked)
For (I = 0 ; I <= intarray. getupperbound ( 0 ); I ++)
{
Intarray [I] = ran. Next ( 0 , 100 );
Lstdata. Items. Add (intarray [I]);
}
If (Rdochar. Checked)
For (I = 0 ; I <= chararray. getupperbound ( 0 ); I ++)
{
Int Charcode;
Charcode = ran. Next ( 0 , 26 );
Chararray [I] = ( Char )( ' A ' + Charcode );
Lstdata. Items. Add (chararray [I]. tostring () + " : ASC code value = " + (( Int ) Chararray [I]). tostring ());
}
Int A, B;
If (Rdocomplex. Checked)
For (I = 0 ; I <= complexarray. getupperbound ( 0 ); I ++)
{
A = ran. Next ( 0 , 10 );
B = ran. Next ( 0 , 10 );
Complexarray [I] = New Complexnum (A, B );
Lstdata. Items. Add (complexarray [I]. tostring () + " Modulo = " + Complexarray [I]. getmod (). tostring ());
}
}
Private Void Btnfillarray_click ( Object Sender, eventargs E)
{
Onbuttonclick (); // Generic version
}
Private Void Btnexit_click ( Object Sender, eventargs E)
{
Application. Exit ();
}
}