菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻

Java 数组参数

原创
05/13 14:22 更新

Java数据类型教程 - Java数组参数

数组参数

我们可以将数组作为参数传递给方法或构造函数。

我们传递给该方法的数组的类型必须是与形式参数类型兼容的赋值。

以下代码显示了如何将Array作为方法参数传递。

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

上面的代码生成以下结果。

图片.png

数组参数参考

因为数组是一个对象,所以它的引用的副本被传递给一个方法。

如果方法更改数组参数,实际参数不受影响。

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));
  }
}

上面的代码生成以下结果。

图片.png

数组参数的元素

存储在数组参数的元素中的值可以在方法中始终更改。

以下代码显示了如何更改方法中的数组参数的元素。

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";
    }
  }
}

上面的代码生成以下结果。

图片.png

例子

以下代码显示如何更改对象数组元素。

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);
    }
  }
}

上面的代码生成以下结果。
图片.png

综合评分:9.9 评分 请对本文进行纠错,及学习过程中有困难疑惑可在此进行讨论