歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java基礎梳理:數組

Java基礎梳理:數組

日期:2017/3/1 9:19:43   编辑:Linux編程

創建數組

  下面這幾種方式都可以創建一個數組

1         int[] a;
2         int[] b = new int[5];
3         String c[] = new String[] { "Hello", "World" };
4         double [] d = new double[6];

  我們比較習慣於第二種方式,第一種只是聲明了數組並未初始化,使用的時候往往會因為忘記將變量初始化而報錯。而第三種、第四種只是寫法習慣上的問題。

  對於數組初始化,數字類型的值默認為0,字符串類型默認為null,布爾類型默認為false。java還提供了一種簡單的方式,對於這種創建方式,數組的大小就是初始化值的個數。所以使用的時候要注意,因為數組一旦創建就無法改變大小,但是數組元素的值是可以改變的。

        int[] numbers={2005,1007,1994,2015};

  當用做返回值時,我們還可以通過不創建變量的情況下直接創建一個匿名數組

1     public String[] getSomethings(String input) {
2         //TODO: do something ...
3         return new String[]{input};
4     }

數組元素的訪問

  數組的中的元素,我們是通過元素的索引去訪問的

        int[] numbers = { 2005, 1007, 1994, 2015 };
        System.out.println(numbers[1]);

  一旦訪問的下標超過數組的索引,就會報出數組索引超過邊界的異常,所以在使用數組的時候這個也是要注意的一點。

  通常我們用for去便利一個數組中的元素,這種方式便於控制數組中的每個元素

        int[] numbers = { 2005, 1007, 1994, 2015 };
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }

  也可以通過foreach的方式

數組常用的一些基本函數

  對於數組的操作,一般都在java.util.Arrays類中

  1.將數組作為字符串輸出

1         int[] numbers = { 2005, 1007, 1994, 2015 };
2         String input = Arrays.toString(numbers);
3         System.out.println(input);

  2.數組的拷貝

        int[] numbers = { 2005, 1007, 1994, 2015 };
        int[] tempNumbers=numbers;//拷貝的是數組的引用
        System.out.println(Arrays.toString(tempNumbers));
        numbers[0]=1999;
        System.out.println(tempNumbers[0]);

  如果是想直接拷貝數組中的所有元素的值,可以通過Arrays.copyOf方法

        int[] numbers = { 2005, 1007, 1994, 2015 };
        int[] tempNumbers=Arrays.copyOf(numbers, 6);//對於多余的元素將被賦予0值
        System.out.println(Arrays.toString(tempNumbers));

  還可以通過System.arraycopy方法

        int[] numbers = { 2005, 1007, 1994, 2015 };
        int[] tempNumbers=new int[5];
        System.arraycopy(numbers, 0, tempNumbers, 1, numbers.length);
        System.out.println(Arrays.toString(tempNumbers));
        numbers[0]=2000;
        System.out.println(tempNumbers[0]);

  3.數組排序

        int[] numbers = { 2005, 1007, 1994, 2015 };
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));

多維數組

  對於二維數組和不規則數組,其實就是把數組的元素再看成一個數組來處理就可以。

        String input="";
        int[][] table={{11,15,9},{16,7,10},{15,12,13}};
        for (int i = 0; i < table.length; i++) {
            int[] temp=table[i];
            for (int j = 0; j < temp.length; j++) {
                input+=String.valueOf(temp[j])+" ";
            }
        }
        System.out.println(input);

簡單應用

  假設一個ATM機器裡有六個錢箱,每個錢箱存放不同的面值,那麼我們該如何初始化這些錢箱呢?一般情況下我們會這麼做

 1 public class CashUnit {
 2     private int value;
 3 
 4     public int getValue() {
 5         return value;
 6     }
 7 
 8     public void setValue(int value) {
 9         this.value = value;
10     }
11 
12     public int getCount() {
13         return count;
14     }
15 
16     public void setCount(int count) {
17         this.count = count;
18     }
19 
20     private int count;
21 }
View Code
1         CashUnit[] cashUnit=new CashUnit[6];
2         cashUnit[0].setValue(100);
3         cashUnit[1].setValue(50);
4         cashUnit[2].setValue(20);
5         cashUnit[3].setValue(10);
6         cashUnit[4].setValue(5);
7         cashUnit[5].setValue(1);

  我麼還可以通過索引對應值的方式去實現

1     public static int[] cashValues = { 100, 50, 20, 10, 5, 1 };
2 
3     public static CashUnit[] createCashUnits() {
4         CashUnit[] cashUnit = new CashUnit[6];
5         for (int i = 0; i < cashUnit.length; i++) {
6             cashUnit[i].setValue(cashValues[i]);
7         }
8         return cashUnit;
9     }

Copyright © Linux教程網 All Rights Reserved