本篇博文最后修改时间:2016年2月29日,22:43。
本篇介绍传递及返回数组。
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、传递及返回数组
1、传递数组
向方法传递数组的条件:
①方法的接收参数必须是符合其类型的数组;
②数组属于引用数据类型。
ps:在把数组传递进方法之后,如果方法对数组本身做了任何修改,修改结果也将保存下来。
范例1:向方法中传递数组
public class ArrayRefDemo01
{
public static void main(String [] args)
{
int temp[3]={1, 3, 5}; //使用静态初始化定义数组
fun(temp); //传递数组引用
for(int One = 0; One < temp.length; One++) //循环输出
{
System.out.println(temp[One] + "、");
}
}
public static void fun(int Two[]) //接收整型数组引用
{
Two[0] = 6; //修改第一个元素的内容
}
}
程序运行结果:
2、返回数组
从方法返回数组的条件:
在返回值类型声明处明确地写出返回的数组类型 。
范例2:使用方法返回一个数组
public class ArrayRefDemo02
{
public static void main(String [] args)
{
int temp[] = fun(); //通过方法实例化数组
print(temp); //向print()方法中传递数组
}
public static void print(int x[])
{
for(int One = 0; One < x.length; One++) //接收数组
{
System.out.print(x[One] + "、"); //循环输出
}
}
public static int[] fun() //此方法返回一个数组引用
{
int ss[5] = {1, 3, 5, 7, 9}; //定义一个数组
return ss; //返回数组
}
}
程序运行结果: