提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在方法内部修改数组参数的元素
import java.util.Arrays;/*来自 N o w J a v a . c o m - 时 代 Java*/ public class Main { public static void main(String[] args) { int[] origNum = {1, 2, 3}; String[] origNames = {"Mike", "John"}; 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 array has at least one element, store 1116 in its first element if (num != null && num.length > 0) { num[0] = 6;//时代Java公众号 - N o w J a v a . c o m 提 供 } }