Java陣列引數


陣列引數

可以將陣列作為引數傳遞給方法或建構函式。傳遞給該方法的陣列的型別必須是與形式引數型別相容的賦值(即,形參和實參的陣列型別要一樣。

以下程式碼顯示了如何將陣列作為方法引數傳遞 -

public class Main {
  public static void main(String[] args) {
    int[] num = { 1, 2 };

    System.out.println("Before  swap");
    System.out.println("#1: " + num[0]);
    System.out.println("#2: " + num[1]);

    swap(num);

    System.out.println("After  swap");
    System.out.println("#1: " + num[0]);
    System.out.println("#2: " + num[1]);
  }
  public static void swap(int[] source) {
    if (source != null && source.length == 2) {
      // Swap the first and the second elements
      int temp = source[0];
      source[0] = source[1];
      source[1] = temp;
    }
  }
}

上面的程式碼生成以下結果。

Before  swap
#1: 1
#2: 2
After  swap
#1: 2
#2: 1

陣列引數參照

因為陣列是一個物件,所以它的參照副本可傳遞給一個方法。如果方法更改陣列引數,實際引數不受影響。

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[] origNum = { 1, 2, 3 };
    System.out.println("Before method  call:" + Arrays.toString(origNum));

    // Pass the array to the method
    tryArrayChange(origNum);

    System.out.println("After method  call:" + Arrays.toString(origNum));
  }

  public static void tryArrayChange(int[] num) {
    System.out.println("Inside method-1:" + Arrays.toString(num));
    // Create and store a new int array in num
    num = new int[] { 10, 20 };

    System.out.println("Inside method?2:" + Arrays.toString(num));
  }
}

上面的程式碼生成以下結果。

Before method  call:[1, 2, 3]
Inside method-1:[1, 2, 3]
Inside method?2:[10, 20]
After method  call:[1, 2, 3]

陣列的元素引數

儲存在陣列引數中的元素的值在方法中可始終更改。以下程式碼顯示了如何在方法中更改陣列引數的元素值。

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[] origNum = { 1, 2, 3 };
    String[] origNames = { "Java", "SQL" };
    System.out.println("Before method  call, origNum:"
        + Arrays.toString(origNum));
    System.out.println("Before method  call, origNames:"
        + Arrays.toString(origNames));

    // Call methods passing the arrays 
    tryElementChange(origNum);
    tryElementChange(origNames);

    System.out.println("After method  call, origNum:"
        + Arrays.toString(origNum));
    System.out.println("After method  call, origNames:"
        + Arrays.toString(origNames));
  }

  public static void tryElementChange(int[] num) {
    if (num != null && num.length > 0) {
      num[0] = -1;
    }
  }

  public static void tryElementChange(String[] names) {
    if (names != null && names.length > 0) {
      names[0] = "T";
    }
  }
}

上面的程式碼生成以下結果。

Before method  call, origNum:[1, 2, 3]
Before method  call, origNames:[Java, SQL]
After method  call, origNum:[-1, 2, 3]
After method  call, origNames:[T, SQL]

範例

以下程式碼顯示如何更改物件陣列元素。

class Item {
    private double price;
    private String name;

    public Item(String name, double initialPrice) {
        this.name = name;
        this.price = initialPrice;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double newPrice) {
        this.price = newPrice;
    }

    public String toString() {
        return "[" + this.name + ", " + this.price + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Item[] myItems = { new Item("Pen", 2.11), new Item("Pencil", 0.10) };
        System.out.println("Before method  call  #1:" + myItems[0]);
        System.out.println("Before method  call  #2:" + myItems[1]);

        // Call the method passing the array of Item
        tryStateChange(myItems);

        System.out.println("After method  call  #1:" + myItems[0]);
        System.out.println("After method  call  #2:" + myItems[1]);
    }

    public static void tryStateChange(Item[] allItems) {
        if (allItems != null && allItems.length > 0) {
            allItems[0].setPrice(0.38);
        }
    }
}

上面的程式碼生成以下結果。

Before method  call  #1:[Pen, 2.11]
Before method  call  #2:[Pencil, 0.1]
After method  call  #1:[Pen, 0.38]
After method  call  #2:[Pencil, 0.1]