Java 數組基礎


數組

  數組(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()方法嗎?

  如下程序:

  

public class ArrayTest
{
      public static void main(String[] args)
      {
             int[] a = {1, 2, 3};
             int[] b = {1, 2, 3};

             System.out.println(a.equals(b));
      }    
}

  輸出結果是false。

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

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

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

  代碼如下:

ArrayEqualsTest.java
import java.util.Arrays;
public class ArrayEqualsTest
{
    //Compare the contents of two int arrays
    public static boolean isEquals(int[] a, int[] b)
    {
        if( a == null || b == null )
        { 
            return false;
        }
        if(a.length != b.length)
        {
            return false;
        }
        for(int i = 0; i < a.length; ++i )
        {
            if(a[i] != b[i])
            {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args)
    {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};

        System.out.println(isEquals(a,b));
        System.out.println(Arrays.equals(a,b));
    }
}

 

數組元素不為基本數據類型時

  數組元素不為基本原生數據類型時,存放的是引用類型,而不是對象本身。當生成對象之后,引用才指向對象,否則引用為null。

  如下列程序:

ArrayTest2.java
public class ArrayTest2
{
    public static void main(String[] args)
    {
        Person[] p = new Person[3];

        //未生成對象時,引用類型均為空
        System.out.println(p[0]);


        //生成對象之后,引用指向對象
        p[0] = new Person(10);
        p[1] = new Person(20);
        p[2] = new Person(30);

        for(int i = 0; i < p.length; i++)
        {
            System.out.println(p[i].age);
        }

    }
}
class Person
{
    int age;
    public Person(int age)
    {
        this.age = age;

    }
}

 

  輸出:

  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];

  如下程序:

 

public class ArrayTest3
{
      public static void main(String[] args)
      {

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

             System.out.println("Is i an Object? "
                           + (i instanceof Object));

             System.out.println("Is i[0] an int[]? "
                           + (i[0] instanceof int[]));

      }
}

 

  輸出結果是兩個true。

 

變長的二維數組

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

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

  如下程序:

 

public class ArrayTest4
{
    public static void main(String[] args)
    {
        //二維變長數組
        int[][] a = new int[3][];
        a[0] = new int[2];
        a[1] = new int[3];
        a[2] = new int[1];

        //Error: 不能空缺第一維大小
        //int[][] b = new int[][3];
    }
}

 

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

  如下程序:

  

public class ArrayTest5
{
    public static void main(String[] args)
    {

        int[][] c = new int[][]{{1, 2, 3},{4},{5, 6, 7, 8}};

        for(int i = 0; i < c.length; ++i)
        {
            for(int j = 0; j < c[i].length; ++j)
            {
                System.out.print(c[i][j]+" ");        
            }

            System.out.println();
        }
        
    }
}

 

  輸出:

  1 2 3

  4

  5 6 7 8


免責聲明!

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



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