Java——數組

來源:互聯網
上載者:User

標籤:類型   color   清理   tin   數組排序   維數   表示   預設值   函數返回   

java中擁有大量的其他方式,可以用於持有對象,但是數組與其他容器最大的區別就是:

  • 效率 

  在java中數組是效率最高的儲存和隨機訪問對象引用序列的方式。

  • 類型

  數組可以持有基本類型,但是使用泛型之前的容器不能。在使用泛型之前,其他容器在處理對象的時候,都將它們視作根類object處理。在建立一個數組的時候就已經確定了持有某種具體類型

  • 大小

  數組的大小在建立的時候就已經確定了,但是容器的大小不是固定的。就比如ArrayList通過建立一個新的執行個體,然後將舊執行個體中的所有的引用移到新執行個體中,從而實現更多空間的分配。

 

一、數組是第一級對象

  無論使用那種類型的數組,數群組識別碼其實只是一個引用,指向在堆中建立的一個真實對象。

  數組可以分為對象數組和類型數組,這兩種數組在使用上幾乎是相同的,唯一的區別就是對象數組儲存的是引用,基本類型直接儲存基本類型的值。

import java.util.Arrays;class fruit{    static long counter = 0;    final long id = counter++;    String name;    public String toString() {        return "fruit" + id;        }}public class Class15 {    public static void main(String[] args) {        fruit[] f = new fruit[5];        System.out.println(Arrays.toString(f));        for(int i = 0; i < f.length; i++) {            f[i] = new fruit();        }        System.out.println(Arrays.toString(f));        int[] i = new int[5];        System.out.println(Arrays.toString(i));            }}

輸出:

[null, null, null, null, null][fruit0, fruit1, fruit2, fruit3, fruit4][0, 0, 0, 0, 0]

新產生一個對象數組的時候,其中所有地點引用都自動初始化為null,並沒有存有對象,但是基本類型數組會存入基本類型的預設值。

 

二、返回一個數組

  如果你想寫一個函數返回一個數組,在C和C++函數返回的是指向這個數組的指標。

  在java中,就可以直接返回一個數組,這一部分是由記憶體回收行程負責的當你需要它的時候它將一直存在,使用完之後就會被清理。

class fruit{    static long counter = 0;    final long id = counter++;    String name;    public String toString() {        return "fruit" + id;        }    static fruit[] getfruit(int i) {        fruit[] results = new fruit[i];        for(int j = 0; j < i; j++) {            results[j] = new fruit();        }        return results;    }}public class Class15 {    public static void main(String[] args) {        fruit[] f = fruit.getfruit(10);        for(int j = 0; j < f.length; j++) {            System.out.print(f[j].toString() + ",");        }            }}

輸出:

fruit0,fruit1,fruit2,fruit3,fruit4,fruit5,fruit6,fruit7,fruit8,fruit9,

 

三、多維陣列。

public class Class15 {    public static void main(String[] args) {        int[][] a = {                {1,2,3},                {4,5,6},        };        System.out.println(Arrays.deepToString(a));        int[][] b = new int[2][3];        System.out.println(Arrays.deepToString(b));        int[][][] c = new int[2][3][4];        System.out.println(Arrays.deepToString(c));            }}

Arrays.deepToString()可以將多維陣列轉換成多個String。

輸出:

[[1, 2, 3], [4, 5, 6]][[0, 0, 0], [0, 0, 0]][[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]

可以看到int[][] b = new int[x][y],就是x行y列的矩陣,int[][][] c = new int[x][y][z]就是x個y行z列的矩陣。

數組從構成矩陣的每一個向量都是可以具有任意的長度(被稱作粗糙數組)

public class Class15 {    public static void main(String[] args) {        Random rand = new Random(47);        int[][][] a = new int[rand.nextInt(5)][][];        for(int i = 0; i < a.length; i++) {            a[i] = new int[rand.nextInt(5)][];            for(int j = 0; j < a[i].length; j++) {                a[i][j] = new int[rand.nextInt(5)];            }        }        System.out.println(Arrays.deepToString(a));    }}

輸出:

[[], [[0], [0], [0, 0, 0, 0]], [[], [0, 0], [0, 0]]]

 

四、Arrays中的一些實用方法

  • equals()用於比較兩個數組是否相等(deepEquals()用於多維陣列),
  • fill()用於填充數組,
  • sort()用於數組排序,
  • binarySearch()用於在已經排序的數組從尋找元素,
  • toString()用於產生數組的String表示,hashCode用於產生數組的散列碼。
public class Class15 {    public static void main(String[] args) {        Random rand = new Random();        int[] i = new int[5];        Arrays.fill(i, 1);        System.out.println("i" + Arrays.toString(i));        int[] j = new int[7];        Arrays.fill(j, 2);        System.out.println("j" + Arrays.toString(j));        System.out.println("i、j對比結果:" + Arrays.equals(i, j));                System.arraycopy(i, 0, j, 0, i.length);        System.out.println("j" + Arrays.toString(j));        for(int k = 0; k < j.length; k++) {            j[k] = rand.nextInt(10);        }        System.out.println("j" + Arrays.toString(j));        Arrays.sort(j);        System.out.println("j排序後" + Arrays.toString(j));        int loc = Arrays.binarySearch(j, 1);        System.out.println("j[" + loc + "]=1");    }}

輸出:

j[2, 2, 2, 2, 2, 2, 2]i、j對比結果:falsej[1, 1, 1, 1, 1, 2, 2]j[5, 1, 6, 3, 7, 3, 9]j排序後[1, 3, 3, 5, 6, 7, 9]j[0]=1

 


 

 

Java——數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.