您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > java基础练习程序
东北石油大学计算机与信息技术学院李勇勇11.ArgsWords.javaimportjava.util.*;//自动装包拆包publicclassArgsWords{publicstaticvoidmain(String[]args){Mapm1=newHashMap();for(inti=0;iargs.length;i++){intfreq=(Integer)m1.get(args[i])==null?0:(Integer)m1.get(args[i]);m1.put(args[i],freq==0?1:freq+1);}System.out.println(m1.size()+distinctwordsdectected:);System.out.println(m1);}}2.BasicGeneric.javaimportjava.util.*;//泛型publicclassBasicGeneric{publicstaticvoidmain(String[]args){ListStringL1=newArrayListString();L1.add(aaa);L1.add(bbb);L1.add(ccc);for(inti=0;iL1.size();i++){Strings=L1.get(i);System.out.println(i);}CollectionStringc=newHashSetString();c.add(aaa);c.add(bbb);c.add(ddd);for(IteratorStringit=c.iterator();it.hasNext();){Strings=it.next();System.out.println(s);}}}东北石油大学计算机与信息技术学院李勇勇2classMyNameimplementsComparableMyName{intage;publicintcompareTo(MyNamemn){if(this.agemn.age)return1;elseif(this.agemn.age)return-1;elsereturn0;}}3.EnhancedFor.javaimportjava.util.*;publicclassEnhancedFor{publicstaticvoidmain(String[]args){intarr[]={1,2,3,4,5};for(inti:arr){System.out.print(i+);}System.out.println(\n);Collectionc=newArrayList();c.add(aaa);c.add(eee);c.add(ff);for(Objecto:c){System.out.print(o+);}}}4.TestArgsWords.javaimportjava.util.*;publicclassTestArgsWords{privatestaticfinalIntegerONE=newInteger(1);publicstaticvoidmain(String[]args){东北石油大学计算机与信息技术学院李勇勇3Mapm=newHashMap();for(inti=0;iargs.length;i++){Integerfreq=(Integer)m.get(args[i]);m.put(args[i],(freq==null?ONE:newInteger(freq.intValue()+1)));}System.out.println(m.size()+distinctwordsdected:);System.out.println(m);}}5.TestArgsWords2.javaimportjava.util.*;publicclassTestArgsWords2{privatestaticfinalIntegerONE=newInteger(1);publicstaticvoidmain(String[]args){MapString,Integerm=newHashMapString,Integer();for(inti=0;iargs.length;i++){Integerfreq=m.get(args[i]);m.put(args[i],(freq==null?ONE:freq+1));}System.out.println(m.size()+distinctwordsdected:);System.out.println(m);}}6.TestAutoBoxing.javaimportjava.util.*;//自动装包、拆包publicclassTestAutoBoxing{publicstaticvoidmain(String[]args){Mapm1=newHashMap();Mapm2=newHashMap();m1.put(one,1);m1.put(two,2);m1.put(three,3);m2.put(A,2);m2.put(B,3);System.out.println(m1.size());System.out.println(m1.containsKey(one));东北石油大学计算机与信息技术学院李勇勇4System.out.println(m1.containsValue(2));if(m1.containsKey(two)){inti=(Integer)m1.get(two);System.out.println(i);}Mapm3=newHashMap();m3.putAll(m2);System.out.println(m3);}}7.TestCollection.javaimportjava.util.*;//导入collection的工具包publicclassTestCollection{//collection接口举例publicstaticvoidmain(String[]args){//Collectionc=newArrayList();Collectionc=newLinkedList();c.add(hello);//里面只能装引用类型,而不能装基础的数据类型c.add(newInteger(100));c.add(newString(fftt));System.out.println(c.size()=+c.size());System.out.println(c);//调用了c的toString()方法}}8.TestComparable.javaimportjava.util.*;publicclassTestComparable{publicstaticvoidmain(String[]args){ListL1=newLinkedList();L1.add(newName(li,si));L1.add(newName(wang,wu));L1.add(newName(zhang,san));System.out.println(L1);System.out.println();Collections.sort(L1);System.out.println(L1);东北石油大学计算机与信息技术学院李勇勇5}}classNameimplementsComparable{privateStringfirstName;privateStringlastName;publicName(StringfirstName,StringlastName){this.firstName=firstName;this.lastName=lastName;}publicStringgetfirstName(){returnfirstName;}publicStringgetlastName(){returnlastName;}//重写Name类的toString方法publicStringtoString(){returnfirstName++lastName;}//重写equals方法publicbooleanequals(Objectobj){if(objinstanceofName){Namename=(Name)obj;returnfirstName.equals(name.firstName)&&(lastName.equals(name.lastName));}returnsuper.equals(obj);}//重写hashCode方法publicinthashCode(){returnfirstName.hashCode();}东北石油大学计算机与信息技术学院李勇勇6//实现了comparable接口,应该重写compareTo这个方法publicintcompareTo(Objecto){Namen=(Name)o;intlastCmp=lastName.compareTo(n.lastName);returnlastCmp!=0?lastCmp:firstName.compareTo(n.firstName);}}9.TestHashSet.javaimportjava.util.*;publicclassTestHashSet{publicstaticvoidmain(String[]args){Sets1=newHashSet();Sets2=newHashSet();s1.add(a);s1.add(b);s1.add(c);s2.add(a);s2.add(b);s2.add(d);Setsn=newHashSet(s1);sn.retainAll(s2);//retainAll求交集Setsm=newHashSet(s1);//求集合的总和,set里面的元素不可以重复sm.addAll(s2);System.out.println(sn);System.out.println(sm);}}10.TestIterator.javaimportjava.util.*;publicclassTestIterator{publicstaticvoidmain(String[]args){Collectionc=newHashSet();c.add(newName(f1,l1));c.add(newName(f2,l2));c.add(newName(f3,l3));东北石油大学计算机与信息技术学院李勇勇7Iteratori=c.iterator();while(i.hasNext()){//next()值类型为object类型,需要将object值类型转化为相应的数据类型Namen=(Name)i.next();System.out.println(n.getFirstName()+);}}}className{privateStringFirstName;privateStringLastName;//构造方法publicName(StringFirstName,StringLastName){this.FirstName=FirstName;this.LastName=LastName;}publicStringgetFirstName(){returnFirstName;}publicStringgetLastName(){returnLastName;}//重写name的toString方法publicStringtoString(){returnFirstName++LastName;}}11.TestList.javaimportjava.util.*;publicclassTestList{publicstaticvoidmain(String[]args)东北石油大学计算机与信息技术学院李勇勇8{ListL1=newLinkedList();ListL
本文标题:java基础练习程序
链接地址:https://www.777doc.com/doc-4851003 .html