还剩5页未读,继续阅读
文本内容:
binarysearch找到匹配的元素值,返回指定元素的索引找不到,返回一个负值回匚代码using system;using system.collections.generic;using system.linq;using system.text;namespace consoleapp1i cati on1class program{static void mainstring[]args{〃查找数组〉binarysearch二进制搜索string[]csharpbooks=new string
[3];csharpbooks
[0]=〃bookl〃;csharpbooks
[1]=〃book3〃;csharpbooks
[2]二〃book2〃;array.sort csharpbooks;//binarysearch要求搜寻的数组必须先排序,并且按升序排序数组中的每个元素要实现【comparable接口foreach stringcsharpbookin csharpbooksconsole.writelinecsharpbook;}console.writeline〃使用二分算法搜寻数组元素值book2:〃;object searchbook=〃book2〃;int searchresult=array.binarysearchcsharpbooks,search book;if searchresult0{console.writeline〃搜寻数组元素值{0},没有找到!〃,searchbook;}else console.writeline〃搜寻数组元素值{0},已找到,在索引位置{1}〃,searchbook,searchresult;console.readline;使用数组〉查找数组》find搜索和indexof搜索回匚代码using system;using system.collections.generic;using system.linq;using system.text;namespace consoleapp1i cati on1class program{static void mainstring[]args〃查找数组〉find搜索string[]csharpbooks={〃bookl〃,〃book2〃,〃book3〃,“book4〃,〃book5〃};〃传递一个数组和一个委托到find方法,find方法将以每个数组元素为参数调用getsharpbook方法string firstcsharpbooks=array.findstringcsharpbooks,getcsharpbook;〃如果找到就定位到第一个元素console.writeline〃找到了数组元素值为{0}的数组元素〃,firstcsharpbooks;//console.readline;//---------------------------------------------------------------------------------------〃查找数组》indexof搜索array indexofcsharpbooks=array.createlnstancetypeofst ring,6;indexofcsharpbooks.setvalue//bookl,/,0;indexofcsharpbooks.setvalue〃book2〃,1;indexofcsharpbooks.setvalue〃book3〃,2;indexofcsharpbooks.setvalue〃book4〃,3;indexofcsharpbooks.setvalue〃book5〃,4;indexofcsharpbooks.setvalue〃book6〃,5;string csharpbook=〃book5〃;int searchresultindex=array.indexofindexofcsharpbooks,csharpbook;console.writeline〃{0}在indexofcsharpbooks数组中首次出现的位置是{1}〃,csharpbook,searchresultindex;〃定义一个执行查找元素的方法private staticbool getcsharpbookstringcsharpbook{if csharpbook==〃book2〃return true;}else returnfalse;使用数组〉反转数组何谓反转数组,就是123木头人,变成人头木321o—一代码using system;using system.collections.generic;using system.linq;using system.text;class program{static voidmainstring[]argsstring[]csharpbooks={〃bookl〃,〃book2〃,〃book5〃,“book7〃,〃book8〃,〃book9〃};console.writeline〃在没有调用reverse的数组顺序为〃;printarraycsharpbooks;〃调用array.reverse反转数组的顺序array.reversecsharpbooks;console.writeline〃调用reverse方法后的数组顺序为〃;printarraycsharpbooks;console.readline;static voidprintarraystring[]arrayforeach stringelement inarrayconsole.writelineelement;使用数组〉复制数组有两种方法,system.array.copy是静态方法,system.array.copyto是实例方法都可以将一个数组的一部分元素复制到另外一个数组中日匚代码using system;using system.collections.generic;using system.text;namespace copyarraystatic voidmainstring[]args string[]csharpbooks={〃bookl〃,〃book2〃,〃book5〃,〃book7〃,〃book8,〃book9〃};string[]csharpbooks2=new string[csharpbooks.length];string[]csharpbooks3=new string[csharpbooks.length];console.writeline〃源数组的内容为〃;printarraycsharpbooks;〃使用copy复制数组array.copycsharpbooks,csharpbooks2,4;console.writeline〃使用copy方法复制到的数组内容为:〃;printarraycsharpbooks2;〃使用copyto复制数组csharpbooks.copytocsharpbooks3,0;console.writeline〃使用copyto方法复制到的数组内容为:〃;printarraycsharpbooks3;console.readline;}static voidprintarraystring[]array{foreach stringelement inarrayconsole.writelineelement;}使用数组〉复制数组这个我也有点看不明白,代码还是先放上吧运行时才检测数组元素的类型以及数组长度m匚代码using system;using system.collections.generic;using system.text;namespace dynamiccreateinstancestaticvoidmainstring[]args〃动态创建一个5个元素的一维string数组array csharpbooks=array.createlnstancetypeofstring,5;for inti=0;i=csharpbooks.getupperbound0;i csharpbooks.setvalue/zbook/z i 1,i;foreach stringcsharpbook incsharpbooksconsole.writelinecsharpbook;console.readline;结构体struct mystrupublicmystruint age,int height{this.age=age;this.height=height;public intage;public intheight;public overridestring tostringreturn nage:” age ”height:” height;;class programstaticvoidmainstring[]args{mystru[]ms=new mystru[]{new mystru12,90,new mystru23,45};fbreach mystrus inms//wcfserviceclient.servicereference
1.service1client ss=newservicereference
1.service1client;//system.console.writeliness.getmystring;。