JAVA數組之Arrays類及其方法

來源:互聯網
上載者:User

標籤:

public class Arraysextends Object此類包含用來運算元組(比如排序和搜尋)的各種方法。此類還包含一個允許將數組作為列表來查看的靜態工廠。

除非特別註明,否則如果指定數組引用為 null,則此類中的方法都會拋出 NullPointerException。

此類中所含方法的文檔都包括對實現 的簡短描述。應該將這些描述視為實現注意事項,而不應將它們視為規範 的一部分。實現者應該可以隨意替代其他演算法,只要遵循規範本身即可。(例如,sort(Object[]) 使用的演算法不必是一個合并排序演算法,但它必須是穩定的。)

此類是 Java Collections Framework 的成員。

 1 import java.util.Arrays; 2 public class TestArrays { 3     public static void main(String[] args) { 4         int[] a = new int[]{11,31,24,98,-9,56,73,-27}; 5         int[] b = new int[]{11,31,24,98,-9,56,73,-27}; 6          7         // static void sort(int[] a) 對指定的 int 型數組按數字升序進行排序。 8         Arrays.sort(a); 9         for(int i = 0; i < a.length; i++){10             System.out.print(a[i] + "\t");11         }12         System.out.println();13         System.out.println();14         15         // static int binarySearch(int[] a, int key)使用二分搜尋法來搜尋指定的 int 型數組,以獲得指定的值。16         int temp = Arrays.binarySearch(a, 73);17         System.out.println(temp);18         System.out.println();19         20         // static int[] copyOf(int[] original, int newLength)21         // 複製指定的數組,截取或用 0 填充(如有必要),以使副本具有指定的長度。22         int[] t = Arrays.copyOf(a, a.length);23         for(int i = 0; i < a.length; i++){24             System.out.print(t[i] + "\t");25         }26         System.out.println();27         System.out.println();28         29         // static boolean equals(int[] a, int[] a2)如果兩個指定的 int 型數組彼此相等,則返回 true。30         boolean bool1 = Arrays.equals(a, t);31         System.out.println(bool1);32         System.out.println();33         boolean bool2 = Arrays.equals(a, b);34         System.out.println(bool2);35         System.out.println();36         37         // static int hashCode(int[] a) 基於指定數組的內容返回雜湊值。38         int s1 = Arrays.hashCode(a);39         int s2 = Arrays.hashCode(b);40         int s3 = Arrays.hashCode(t);41         System.out.println(s1);42         System.out.println(s2);43         System.out.println(s3);44         System.out.println();45         46         // static String toString(int[] a) 返回指定數組內容的字串表示形式。47         String str = Arrays.toString(a);48         System.out.println(str);49     }50 }

程式運行結果如下:

-27    -9    11    24    31    56    73    98    6-27    -9    11    24    31    56    73    98    truefalse-678226926-1591174308-678226926[-27, -9, 11, 24, 31, 56, 73, 98]

注意:Arrays 類的 equals 方法比較的是兩個數組的 hashCode 值。若兩個 hashCode 值相同,說明這兩個數組是同一個數組,返回 true ,否則,返回 false 。

    if(a.hashCode == b.hashCode)        return true;    else        return false;

 

JAVA數組之Arrays類及其方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.