Java 數組基礎

來源:互聯網
上載者:User

標籤:java   strong   列表   基礎   程式   不同   決定   isequal   length   

數組

  數組(Array):相同類型資料的集合。

定義數組

  方式1(推薦,更能表明數群組類型)

  type[] 變數名 = new type[數組中元素的個數];

  比如:

  int[] a = new int[10];

  數組名,也即引用a,指向數組元素的首地址。

  方式2(同C語言)

  type變數名[] = new type[數組中元素的個數];

  如:

  int a[] = new int[10];

  方式3 定義時直接初始化

  type[] 變數名 = new type[]{逗號分隔的初始化值};

  其中紅色部分可省略,所以又有兩種:

  int[] a = {1,2,3,4};

  int[] a = new int[]{1,2,3,4};

  其中int[] a = new int[]{1,2,3,4};的第二個方括弧中不能加上數組長度,因為元素個數是由後面花括弧的內容決定的。

 數組運用基礎

數組長度

  Java中的每個數組都有一個名為length的屬性,表示數組的長度。

  length屬性是public final int的,即length是唯讀。數組長度一旦確定,就不能改變大小。

equals()

  數組內容的比較可以使用equals()方法嗎?

  如下程式:

 1 public class ArrayTest 2 { 3       public static void main(String[] args) 4       { 5              int[] a = {1, 2, 3}; 6              int[] b = {1, 2, 3}; 7  8              System.out.println(a.equals(b)); 9       }    10 }

 輸出結果是false。

  所以證明不能直接用equals()方法比較數組內容,因為沒有override Object中的實現,所以仍採用其實現,即採用==實現equals()方法,比較是否為同一個對象。

  怎麼比較呢?一種解決方案是自己寫代碼,另一種方法是利用java.util.Arrays。

  java.util.Arrays中的方法全是static的。其中包括了equals()方法的各種重載版本。

  代碼如下:

 1 ArrayEqualsTest.java  2  3 import java.util.Arrays; 4 public class ArrayEqualsTest 5 { 6     //Compare the contents of two int arrays 7     public static boolean isEquals(int[] a, int[] b) 8     { 9         if( a == null || b == null )10         { 11             return false;12         }13         if(a.length != b.length)14         {15             return false;16         }17         for(int i = 0; i < a.length; ++i )18         {19             if(a[i] != b[i])20             {21                 return false;22             }23         }24         return true;25     }26 27     public static void main(String[] args)28     {29         int[] a = {1, 2, 3};30         int[] b = {1, 2, 3};31 32         System.out.println(isEquals(a,b));33         System.out.println(Arrays.equals(a,b));34     }35 }

數組元素不為基礎資料型別 (Elementary Data Type)時

  數組元素不為基本原生資料類型時,存放的是參考型別,而不是對象本身。當產生對象之後,引用才指向對象,否則引用為null。

  如下列程式:

 1 ArrayTest2.java 2  3 public class ArrayTest2 4 { 5     public static void main(String[] args) 6     { 7         Person[] p = new Person[3]; 8  9         //未產生對象時,參考型別均為空白10         System.out.println(p[0]);11 12 13         //產生對象之後,引用指向對象14         p[0] = new Person(10);15         p[1] = new Person(20);16         p[2] = new Person(30);17 18         for(int i = 0; i < p.length; i++)19         {20             System.out.println(p[i].age);21         }22 23     }24 }25 class Person26 {27     int age;28     public Person(int age)29     {30         this.age = age;31 32     }33 }

輸出:

  null

  10

  20

  30

  也可以在初始化列表裡面直接寫:

  Person[] p = new Person[]{new Person(10), new Person(20), new Person(30)};

二維數組

  二維數組是數組的數組。

 

二維數組基礎

  基本的定義方式有兩種形式,如:

  type[][] i = new type[2][3];(推薦)

  type i[][] = new type[2][3];

  如下程式:

 1 public class ArrayTest3 2 { 3       public static void main(String[] args) 4       { 5  6              int[][] i = new int[2][3]; 7  8              System.out.println("Is i an Object? " 9                            + (i instanceof Object));10 11              System.out.println("Is i[0] an int[]? "12                            + (i[0] instanceof int[]));13 14       }15 }

 輸出結果是兩個true。

 

變長的二維數組

  二維數組的每個元素都是一個一維數組,這些數組不一定都是等長的。

  聲明二維數組的時候可以只指定第一維大小,空缺出第二維大小,之後再指定不同長度的數組。但是注意,第一維大小不能空缺(不能只指定列數不指定行數)。

  如下程式:

 1 public class ArrayTest4 2 { 3     public static void main(String[] args) 4     { 5         //二維變長數組 6         int[][] a = new int[3][]; 7         a[0] = new int[2]; 8         a[1] = new int[3]; 9         a[2] = new int[1];10 11         //Error: 不能空缺第一維大小12         //int[][] b = new int[][3];13     }14 }

 二維數組也可以在定義的時候初始化,使用花括弧的嵌套完成,這時候不指定兩個維數的大小,並且根據初始化值的個數不同,可以產生不同長度的數組元素。

  如下程式:

 1 public class ArrayTest5 2 { 3     public static void main(String[] args) 4     { 5  6         int[][] c = new int[][]{{1, 2, 3},{4},{5, 6, 7, 8}}; 7  8         for(int i = 0; i < c.length; ++i) 9         {10             for(int j = 0; j < c[i].length; ++j)11             {12                 System.out.print(c[i][j]+" ");        13             }14 15             System.out.println();16         }17         18     }19 }

 輸出:

  1 2 3

  4

  5 6 7 8

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.