012java中兩個數組的比較、填充、查找、復制


原文地址:http://c.biancheng.net/view/919.html

(一)java中兩個數組的比較

相等的條件:

  1. 元素個數相等;
  2. 對應位置對應的元素也相等

比較方法:equals(arrayA, arrayB)

arrayA 是用於比較的第一個數組,arrayB 是用於比較的第二個數組

Arrays.equals(arrayA, arrayB);

例1:定義 3 個數組,分別為 score1、score2 和 score3。第一個數組直接給出了數組的值;第二個數組先定義數組的長度,然后為每個元素賦值;第三個數組中的元素和第一個數組中的元素相同,但是順序不同。分別將 score1 數組與 score2 和 score3 數組進行比較,並輸出比較的結果。

public static void main(String[] args) {
    double[] score1 = { 99, 100, 98.5, 96.5, 72 };
    double[] score2 = new double[5];
    score2[0] = 99;
    score2[1] = 100;
    score2[2] = 98.5;
    score2[3] = 96.5;
    score2[4] = 72;
    double[] score3 = { 99, 96.5, 98.5, 100, 72 };
    if (Arrays.equals(score1, score2)) {
        System.out.println("score1 數組和 score2 數組相等");
    } else {
        System.out.println("score1 數組和 score2 數組不等");
    }
    if (Arrays.equals(score1, score3)) {
        System.out.println("score1 數組和 score3 數組相等");
    } else {
        System.out.println("score1 數組和 score3 數組不等");
    }
}

運行輸出結果如下:

score1 數組和 score2 數組相等
score1 數組和 score3 數組不等


(二)java中數組的填充fill()
java中 使用fill() 方法可以在指定位置進行數值填充,但只能使用同一個數值進行填充

Arrays.fill(array,value);

例1:聲明一個 int 類型的 number 數組,然后通過 for 語句進行遍歷,在該語句中調用 Arrays 類的 fill() 方法來填充數組,並輸出數組中元素的值。

 
        
public static void main(String[] args) {
    int[] number = new int[5];
    System.out.println("number —共有 " + number.length + " 個元素,它們分別是:");
    for (int i = 0; i < number.length; i++) {
        Arrays.fill(number, i);
        System.out.println("number[" + i + "]=" + i);
    }
}
 
        

執行上述代碼,輸出結果如下所示。

number 一共有 5 個元素,它們分別是:
number[0]=0
number[1]=1
number[2]=2
number[3]=3
number[4]=4
 
        
(三)java中數組的查找指定元素binarySearch()
查找數組是指從數組中查詢指定位置的元素,或者查詢某元素在指定數組中的位置。
binarySearch() 方法可以實現數組的查找,該方法可使用二分搜索法來搜索指定數組,以獲得指定對象,該方法返回要搜索元素的索引值。
binarySearch() 方法有多種重載形式來滿足不同類型數組的查找需要,常用的重載形式有兩種。
(1) 第一種形式如下:a 表示要搜索的數組,key 表示要搜索的值。如果 key 包含在數組中,則返回搜索值的索引;否則返回 -1 或“-插入點”。
插入點指搜索鍵將要插入數組的位置,即第一個大於此鍵的元素索引。
binarySearch(Object[] a,Object key);
 
        

例1:聲明 double 類型的 score 數組,接着調用 Arrays 類的 sort() 方法對 score 數組排序,排序后分別查找數組中值為 100 和 60 的元素,分別將結果保存到 index1 和 index2 變量中,最后輸出變量的值。代碼如下:

 
        
public static void main(String[] args) {
    double[] score = { 99.5, 100, 98, 97.5, 100, 95, 85.5, 100 };
    Arrays.sort(score);
    int index1 = Arrays.binarySearch(score, 100);
    int index2 = Arrays.binarySearch(score, 60);
    System.out.println("查找到 100 的位置是:" + index1);
    System.out.println("查找到 60 的位置是:" + index2);
}
執行上述代碼,輸出結果如下:
查找到 100 的位置是:5
查找到 60 的位置是:-1

(2) 除了上述形式外,binarySearch() 還有另一種常用的形式,這種形式用於在指定的范圍內查找某一元素。語法如下:
binarySearch(Object[] a,int fromIndex,int toIndex,Object key);
其中,a表示要進行查找的數組,fromIndex 指定范圍的開始處索引(包含開始處),toIndex 指定范圍的結束處索引(不包含結束處),key 表示要搜索的元素。

在使用 binarySearch()方法的上述重載形式時,也需要對數組進行排序,以便獲取准確的索引值。如果要查找的元素 key 在指定的范圍內,則返回搜索鍵的索引;否則返回 -1 或 “-插入點”。插入點指要將鍵插入數組的位置,即范圍內第一個大於此鍵的元素索引。

例 2 :對例 1 中創建的 score 數組進行查找元素,指定開始位置為 2,結束位置為 6。代碼如下:

public static void main(String[] args) {
    double[] score = {99.5,100,98,97.5,100,95,85.5,100};
    Arrays.sort(score);
    int index1 = Arrays.binarySearch(score,2,6,100);
    int index2 = Arrays.binarySearch(score,2,6,60);
    System.out.println("查找到 100 的位置是:"+index1);
    System.out.println("查找到 60 的位置是:"+ index2);
}

執行上述代碼,輸出結果如下:

查找到 100 的位置是:5
查找到 60 的位置是:-3
注意:實現對數組進行查找的方法很多,但是使用 Arrays 對象的 binarySearch() 方法是最簡單、最方便的一種,因此該方法經常被應用。
(四)java中數組的復制arraycopy()方法、clone() 方法、copyOf()和copyOfRan
概念:復制數組,是指將一個數組中的元素在另一個數組中進行復制。
在 Java 中實現數組復制分別有以下 4 種方法:
  1. Arrays 類的 copyOf() 方法
  2. Arrays 類的 copyOfRange() 方法
  3. System 類的 arraycopy() 方法
  4. Object 類的 clone() 方法
copyOf() 方法是復制數組至指定長度,copyOfRange() 方法則將指定數組的指定長度復制到一個新數組中
 
        
  1. Arrays 類的 copyOf() 方法
格式:
Arrays.copyOf(dataType[] srcArray,int length);
srcArray 表示要進行復制的數組,length 表示復制后的新數組的長度。

使用這種方法復制數組時,默認從原數組的第一個元素(索引值為 0)開始復制,目標數組的長度將為 length。
如果 length 大於 srcArray.length,則目標數組中采用默認值填充;
如果 length 小於 srcArray.length,則復制到第 length 個元素(索引值為 length-1)即止。


注意:目標數組如果已經存在,將會被重構。
例1:假設有一個數組中保存了 5 個成績,現在需要在一個新數組中保存這 5 個成績,同時留 3 個空余的元素供后期開發使用。

import java.util.Arrays;
public class Test19{
    public static void main(String[] args) {
        // 定義長度為 5 的數組
        int scores[] = new int[]{57,81,68,75,91};
        // 輸出原數組
        System.out.println("原數組內容如下:");
        // 循環遍歷原數組
        for(int i=0;i<scores.length;i++) {
            // 將數組元素輸出
            System.out.print(scores[i]+"\t");
        }
        // 定義一個新的數組,將 scores 數組中的 5 個元素復制過來
        // 同時留 3 個內存空間供以后開發使用
        int[] newScores = (int[])Arrays.copyOf(scores,8);
        System.out.println("\n復制的新數組內容如下:");
        // 循環遍歷復制后的新數組
        for(int j=0;j<newScores.length;j++) {
            // 將新數組的元素輸出
            System.out.print(newScores[j]+"\t");
        }
    }
}

因為原數組 scores 的數據類型為 int,而使用 Arrays.copyOf(scores,8) 方法復制數組之后返回的是 Object[] 類型,因此需要將 Object[] 數據類型強制轉換為 int[] 類型。同時,也正因為 scores 的數據類型為 int,因此默認值為 0。

運行的結果如下所示。

原數組內容如下:
57    81    68    75    91   
復制的新數組內容如下:
57    81    68    75    91    0    0    0

2. Arrays 類的 copyOfRange() 方法
語法:
  • srcArray 表示原數組。
  • startIndex 表示開始復制的起始索引,目標數組中將包含起始索引對應的元素,另外,startIndex 必須在 0 到 srcArray.length 之間。
  • endIndex 表示終止索引,目標數組中將不包含終止索引對應的元素,endIndex 必須大於等於 startIndex,可以大於 srcArray.length,如果大於 srcArray.length,則目標數組中使用默認值填充。
  • 注意:目標數組如果已經存在,將會被重構。
Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)

3.  System 類的 arraycopy() 方法

語法
System.arraycopy(dataType[] srcArray,int srcIndex,int destArray,int destIndex,int length)
 
        
srcArray 表示原數組;
srcIndex 表示原數組中的起始索引;
destArray 表示目標數組;
destIndex 表示目標數組中的起始索引;
length 表示要復制的數組長度。

使用此方法復制數組時,length+srcIndex 必須小於等於 srcArray.length,
同時 length+destIndex 必須小於等於 destArray.length。

注意:目標數組必須已經存在,且不會被重構,相當於替換目標數組中的部分元素。
4.使用 clone() 方法
clone() 方法也可以實現復制數組。該方法是類 Object 中的方法,可以創建一個有單獨內存空間的對象。
因為數組也是一個 Object 類,因此也可以使用數組對象的 clone() 方法來復制數組。

clone() 方法的返回值是 Object 類型,要使用強制類型轉換為適當的類型。
語法:
array_name.clone()
 
        

示例語句如下:

int[] targetArray=(int[])sourceArray.clone();

注意:目標數組如果已經存在,將會被重構。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM