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