一维数组做为参数:数组
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); } }