資源描述:
《java技術(shù)從入門到精通學(xué)習(xí)筆記》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、數(shù)組的相關(guān)操作l在Java中,所有的數(shù)組都有一個缺省的屬性length,用于獲取數(shù)組中元素的個數(shù)。l數(shù)組的復(fù)制:System.arraycopy()。l數(shù)組的排序:Arrays.sort()。l在已排序的數(shù)組中查找某個元素:Arrays.binarySearch()。arraycopy(在java.lang.System.目錄下)數(shù)組元素的復(fù)制publicstaticvoidarraycopy(Object?src,//要拷貝的數(shù)組對象int?srcPos,/*從數(shù)組對象的哪個位置開始拷貝元素*/Object?des
2、t,//目的數(shù)組int?destPos,/*從目的數(shù)組的哪個位置存放拷貝過來的元素*/int?length)//要拷貝元素的個數(shù)classArrayTest{publicstaticvoidmain(String[]args){int[]num1=newint[]{1,2,3};int[]num2=newint[3];System.arraycopy(num1,0,num2,0,num1.length);for(inti=0;i3、);}}}運行結(jié)果為:123classArrayTest{publicstaticvoidmain(String[]args){Point[]pts1=newPoint[]{newPoint(1,1),newPoint(2,2),newPoint(3,3)};Point[]pts2=newPoint[3];System.arraycopy(pts1,0,pts2,0,pts1.length);for(inti=0;i4、+","+"y="+pts2[i].y);}pts2[1].x=5;pts2[1].y=5;System.out.println("x="+pts2[1].x+","+"y="+pts2[1].y);}}classPoint{intx,y;Point(intx,inty){this.x=x;this.y=y;}}運行結(jié)果為:x=1,y=1x=2,y=2x=3,y=3x=5,y=5sort(?在java.util.Arrays目錄下)數(shù)組的排序importjava.util.Arrays;classArrayTest{
5、publicstaticvoidmain(String[]args){int[]num=newint[]{1,4,3,2};Arrays.sort(num);for(inti=0;i6、t{intx,y;Point(intx,inty){this.x=x;this.y=y;}}運行結(jié)果為:1234index=1element=2importjava.util.Arrays;classArrayTest{publicstaticvoidmain(String[]args){Student[]ss=newStudent[]{newStudent(3,"Chenping"),newStudent(1,"Zhangli"),newStudent(2,"Liming")};Arrays.sort(ss);fo
7、r(inti=0;i8、dent)o;returnnum>s.num?1:(num==s.num?0:-1);}}運行結(jié)果為:num=1,Zhanglinum=2,Limingnum=3,Chenpingimportjava.util.Arrays;classArrayTest{publicstaticvoidmain(String[]args){Student[]s