方法中数组参数传递 1旧c1assA 2345678 int b[] void method1 (int a[]) Syste. out. print("b数组的元素分别为:") for (int i=0: i<b length: i++) 11 System. out. print(b[i]+"") 12 3 System. out. println() 14 15- 16 17旧pub1 ic class test public static void main(String args[]) 20 21 int c[]=new int[6] 22 for (int i=0:i<c. length: i++) 24 c[i]=1+1 25 26 A al=new A( 29 a1. method1(c)://效组传递给 30 31
方法中数组参数传递
E public class StringArray public static void mb swap(string[]s) if(slength<2) return String t=s[0]: s[0]=s[1]: s[1]=t: public static void main(String args[]) String[] s=["1", 2 mb_ swap(s) System. out. println(s[0]+s[1])
Continue 假如自己已经有 Circle类 声明创建如下: Circle c[-new Circle(5) c为数组,里面有5个元素,每个元素的类型为 Circle 注意:现在还不能直接使用c[0]c[1]-.,因为还没 有创建对象。 例如: Circle cl;/cl只是声明了变量,还不是具体的 个对象,还要通过new运算 所以,还缺少: cLO]F=new Circle
Continue • 假如自己已经有Circle 类 • 声明创建如下: – Circle c[]=new Circle(5); – c为数组,里面有5个元素,每个元素的类型为Circle – 注意:现在还不能直接使用c[0],c[1]…,因为还没 有创建对象。 – 例如:Circle c1;//c1只是声明了变量,还不是具体的 一个对象,还要通过 new 运算 – 所以,还缺少: • c[0]=new Circle(); ……
class Circle double r Circle() Circle(double r) thisrr double getArea( return Math. Pi*r*r E public class TestCircle public static void main(String args[]) Circle c1 c1= new Circle():/必须创建对象才能去调用里面的方法 Circle c[]= new Circle[3]:/ Circle类型的效组,这里的neW是为了创建效组对象,而不是创建neW对象 //很多时候用循环来创建对象 for(int 1=0; i<c length: 1++) c[i]= new Circle(i)://创建了对象,半径的值分别为对应的i for (int 1=0: i<c. length: i++) System,out. print1n("第"+i+"个圆的面积为:"+c[i]. geaRed())
数组作为类的成员变量 数组当然也可以是类的成员变量 class a int xl Circle cl;/ Circle是自己编写的类
数组作为类的成员变量 • 数组当然也可以是类的成员变量 class a { int x[]; Circle c[];//Circle是自己编写的类 }