溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java數(shù)組常用方法的應(yīng)用

發(fā)布時(shí)間:2020-06-23 13:22:27 來源:億速云 閱讀:163 作者:元一 欄目:編程語言

這期內(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ù)組長度小于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ù)制長度為length,若長度超過數(shù)組原長,則超出元素為默認(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ù)組長度為 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è)長度為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)注億速云行業(yè)資訊。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI