实验7:数组
姚远
2012-4-6
实验目的
1、理解数组、数组对象。
2、学会2种for语句访问数组.
3、掌握对象数组极其应用。
实验内容
1.修改程序题
public class Test {
public static void main(String[] args)
{
int[] a = new int [10];
int[] b = new int[10];
System.out.println("个数为:"+a.length);
for (int i=0;i { a[i]= i*10; } for (int i=0;i { System.out.print(b[i]+" " ); } } } 要求: 阴影处代码保持不变,修改上述程序,使之运行结果为为:个数为:10 0 10 20 30 40 50 60 70 80 90 public class Test { public static void main(String[] args) { int[] a = new int [10]; int[] b = new int[10]; System.out.println("个数为:"+a.length); a=b; for (int i=0;i { a[i]= i*10; } for (int i=0;i { System.out.print(b[i]+" " ); } } } 2.程序填空题 public class Test { public static void main(String[] args) { int[] a = new int [10]; System.out.println("个数为:"+a.length); for (int i=0;i { a[i]= i*10; } for (int value: a) { System.out.print(value+""); } } } 完成填空处代码,程序运行结果为: 个数为:10 0 10 20 30 40 50 60 70 80 90 3.程序填空题 如下程序拟通过循环创建10个Value对象,这是个Value对象应有一个数组进行管理。每个对象中的i值分别赋值为0、1、2。。。9 在测试类main方法的第二个for循环中应能将数组中的每个元素(每个Value对象)中的i值输出。运行结果为: val =0 val =1 val =2 val =3 val =4 val =5 val =6 val =7 val =8 val =9 class Value { private int i=1; public int getI() { return i; } public void setI(int i) { this.i =this.i*i; } } public class Test{ public static void main(String[] args) { Value [] av = ; for (int { av[i]= ; ; } for (Value val :av) { System.out.println("val ="+ ); } } } 改 public class Test{ public static void main(String[] args) { Value [] av = new Value[10] ; for (int i=0;i { av[i]=new Value() ; av[i].setI(i) ; } for (Value val :av) { System.out.println("val ="+ val.getI() ); } } }