复制代码
- //:Array1.java
- //Testingthesorting&searchinginArrays
- packagec08.newcollections;
- importjava.util.*;
- publicclassArray1{
- staticRandomr=newRandom();
- staticStringssource=
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
- "abcdefghijklmnopqrstuvwxyz";
- staticchar[]src=ssource.toCharArray();
- //CreatearandomString
- publicstaticStringrandString(intlength){
- char[]buf=newchar[length];
- intrnd;
- for(inti=0;i<length;i++){
- rnd=Math.abs(r.nextInt())%src.length;
- buf[i]=src[rnd];
- }
- returnnewString(buf);
- }
- //CreatearandomarrayofStrings:
- publicstatic
- String[]randStrings(intlength,intsize){
- String[]s=newString[size];
- for(inti=0;i<size;i++)
- s[i]=randString(length);
- returns;
- }
- publicstaticvoidprint(byte[]b){
- for(inti=0;i<b.length;i++)
- System.out.print(b[i]+"");
- System.out.println();
- }
- publicstaticvoidprint(String[]s){
- for(inti=0;i<s.length;i++)
- System.out.print(s[i]+"");
- System.out.println();
- }
- publicstaticvoidmain(String[]args){
- byte[]b=newbyte[15];
- r.nextBytes(b);//Fillwithrandombytes
- print(b);
- Arrays.sort(b);
- print(b);
- intloc=Arrays.binarySearch(b,b[10]);
- System.out.println("Locationof"+b[10]+
- "="+loc);
- //TestStringsort&search:
- String[]s=randStrings(4,10);
- print(s);
- Arrays.sort(s);
- print(s);
- loc=Arrays.binarySearch(s,s[4]);
- System.out.println("Locationof"+s[4]+
- "="+loc);
- }
- }///:~
复制代码
- //:AlphaComp.java
- //UsingComparatortoperformanalphabeticsort
- packagec08.newcollections;
- importjava.util.*;
- publicclassAlphaCompimplementsComparator{
- publicintcompare(Objecto1,Objecto2){
- //AssumeitsusedonlyforStrings...
- Strings1=((String)o1).toLowerCase();
- Strings2=((String)o2).toLowerCase();
- returns1.compareTo(s2);
- }
- publicstaticvoidmain(String[]args){
- String[]s=Array1.randStrings(4,10);
- Array1.print(s);
- AlphaCompac=newAlphaComp();
- Arrays.sort(s,ac);
- Array1.print(s);
- //MustusetheComparatortosearch,also:
- intloc=Arrays.binarySearch(s,s[3],ac);
- System.out.println("Locationof"+s[3]+
- "="+loc);
- }
- }///:~
复制代码
- //:CompClass.java
- //AclassthatimplementsComparable
- packagec08.newcollections;
- importjava.util.*;
- publicclassCompClassimplementsComparable{
- privateinti;
- publicCompClass(intii){i=ii;}
- publicintcompareTo(Objecto){
- //Implicitlytestsforcorrecttype:
- intargi=((CompClass)o).i;
- if(i==argi)return0;
- if(i<argi)return-1;
- return1;
- }
- publicstaticvoidprint(Object[]a){
- for(inti=0;i<a.length;i++)
- System.out.print(a[i]+"");
- System.out.println();
- }
- publicStringtoString(){returni+"";}
- publicstaticvoidmain(String[]args){
- CompClass[]a=newCompClass[20];
- for(inti=0;i<a.length;i++)
- a[i]=newCompClass(
- (int)(Math.random()*100));
- print(a);
- Arrays.sort(a);
- print(a);
- intloc=Arrays.binarySearch(a,a[3]);
- System.out.println("Locationof"+a[3]+
- "="+loc);
- }
- }///:~
复制代码
- //:ListSort.java
- //SortingandsearchingListswithCollections
- packagec08.newcollections;
- importjava.util.*;
- publicclassListSort{
- publicstaticvoidmain(String[]args){
- finalintSZ=20;
- //Using"naturalcomparisonmethod":
- Lista=newArrayList();
- for(inti=0;i<SZ;i++)
- a.add(newCompClass(
- (int)(Math.random()*100)));
- Collection1.print(a);
- Collections.sort(a);
- Collection1.print(a);
- Objectfind=a.get(SZ/2);
- intloc=Collections.binarySearch(a,find);
- System.out.println("Locationof"+find+
- "="+loc);
- //UsingaComparator:
- Listb=newArrayList();
- for(inti=0;i<SZ;i++)
- b.add(Array1.randString(4));
- Collection1.print(b);
- AlphaCompac=newAlphaComp();
- Collections.sort(b,ac);
- Collection1.print(b);
- find=b.get(SZ/2);
- //MustusetheComparatortosearch,also:
- loc=Collections.binarySearch(b,find,ac);
- System.out.println("Locationof"+find+
- "="+loc);
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |