一維數組做爲參數:數組
public class Test { //數組做爲參數時,能夠傳遞3中形式 public void m1(int[] a) { System.out.println("數組長度是:"+ a.length); } public static void main(String[] args) { Test t = new Test(); //建立一個數組,傳遞數組引用 int[] b = {1,2,3,4,5}; t.m1(b); //直接建立數組傳值 t.m1(new int[]{1,2,3}); //直接傳遞null,可是次數組不可用 t.m1(null); } }
一維數組做爲返回值:spa
public class Test { //返回數組的引用 public String[] m1() { String[] s = {"abc","de"}; return s; } //返回直接建立的數組 public String[] m2() { return new String[]{"a", "b","c"}; } //返回null public String[] m3() { return null; } public static void main(String[] args) { Test t = new Test(); String[] s1 = t.m1(); System.out.println("接收到的數組長度:" + s1.length); String[] s2 = t.m2(); System.out.println("接收到的數組長度:" + s2.length); String[] s3 = t.m3(); System.out.println("接收到的數組長度:" + s3.length); } }