這期內(nèi)容當中小編將會給大家?guī)碛嘘Pjava數(shù)組常用方法的應用,以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1. arraycopy
方法原型:
public static void arraycopy(sourceArray,int index1,copyArray,index2,int length)
即從sourceArray的index1位置開始,后面length個元素,放到copyArray數(shù)組從index2的位置
注意:這里的index1,2都是數(shù)組的索引,即數(shù)組的下標
如果copyArray數(shù)組長度小于length,程序會崩潰。
實例:創(chuàng)建main方法
void test_arraycopy() { int []a = {1,2,3,4,5}; int []b = {6,7,8,9,10}; System.arraycopy(a, 2, b, 3, 2); System.out.println("\n通過復制以后的到的數(shù)組為:"); for(int i:b) { System.out.printf("%d ",i); } System.out.println(); }
運行結果:
通過復制以后的到的數(shù)組為: 6 7 8 3 4
2. copyOf和copyOFRange方法
copyOf方法原型:
public static float[] copyOf(float []original,int newLength)
從數(shù)組的第一個元素開始復制,復制長度為length,若長度超過數(shù)組原長,則超出元素為默認值0,該方法返回一個數(shù)組。
void test_copyOf() { int []a = {11,22,33,44,55}; int []b = Arrays.copyOf(a, 7); System.out.println("測試copyOf函數(shù),復制后得到的b數(shù)組為"); for(int i:b) { System.out.printf("%d ",i); } System.out.println("\n通過toString方法輸出數(shù)組\n"+Arrays.toString(b)); }
運行結果:
測試copyOf函數(shù),復制后得到的b數(shù)組為 11 22 33 44 55 0 0
原數(shù)組長度為 5 ,length為7,故復制后的數(shù)組最后兩位為默認值 0。
copyOfRange方法原型:
public static double[] copyOfRange(double []original,int from,int to)
從original下標為from的位置開始復制,到to-1的位置結束,返回一個長度為to-from的數(shù)組。
void test_arrayOfRange() { int []a = {55,33,44,22,11}; int []b = Arrays.copyOfRange(a, 1, 4); System.out.println("測試copyOfRange方法:"); System.out.println(Arrays.toString(b)); }
運行結果:
測試copyOfRange方法: [33, 44, 22]
3. 改進遍歷數(shù)組的方法
Arrays.toString(數(shù)組名)
for(循環(huán)體,數(shù)組名) { System.out.println(i); }
或者用 Arrays.toString(數(shù)組名)方法
void print_array() { int []a = {1,2,3,4,5}; System.out.println("采用改進方法遍歷數(shù)組a,輸出結果:"); for(int i:a) { System.out.printf("%d ",i); } System.out.println("調(diào)用toString方法輸出數(shù)組b"); System.out.println(Arrays.toString(b)); }
運行結果:
采用改進方法遍歷數(shù)組a,輸出結果: 1 2 3 4 5 調(diào)用toString方法輸出數(shù)組b [1, 2, 3, 4, 5]
4. 數(shù)組的排序:sort方法
該方法有兩個函數(shù)原型:
public static void sort(doule a[]) public static void sort(doule a[],int start,int end);
第一種,將數(shù)組按升序全排序
第二種從索引為start到索引為end-1的位置,升序排序
void test_arrayOfRange() { int []a = {55,33,44,22,11}; int []b = Arrays.copyOfRange(a, 1, 4); Arrays.sort(a, 1, 4); Arrays.sort(b); System.out.println("排序后b數(shù)組為:"); for(int i:b) { System.out.printf("%d ",i); } System.out.println("\n排序后a數(shù)組為:"); for(int i:a) { System.out.printf("%d ",i); } System.out.println(); }
運行結果:
排序后b數(shù)組為: 22 33 44 排序后a數(shù)組為: 55 22 33 44 11
5. 在數(shù)組中查找一個數(shù)的方法:binarySearch
方法原型:
public static int binarySearch(double [] a,double number)
返回要尋找的數(shù)number的索引,若沒查找到,則返回一個負數(shù)。
void test_binarySearch() { int a[] = {1,2,3}; int x; x= Arrays.binarySearch(a, 2); System.out.println("數(shù)組a為:"); System.out.println(Arrays.toString(a)); System.out.println("數(shù)字x在數(shù)組中的索引(下標)為:"+x); }
運行結果:
數(shù)組a為: [1, 2, 3] 數(shù)字x在數(shù)組中的索引(下標)為:1
6. ArrayList轉數(shù)組:ArrayList
String
[] stringArray = { "a", "b", "c", "d", "e" };ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));String[] stringArr = new String[arrayList.size()];arrayList.toArray(stringArr);for (String s : stringArr)
System.out.println(s);
上述就是小編為大家分享的java數(shù)組常用方法的應用了,如果您也有類似的疑惑,不妨參照上述方法進行嘗試。如果想了解更多相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊。
分享名稱:java數(shù)組常用方法的應用-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.rwnh.cn/article22/pjgjc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、網(wǎng)站設計、云服務器、網(wǎng)站改版、網(wǎng)站收錄、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容