第八周作业
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]•
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]=new int[]{10,20,30,40,50}; 8 System.out.println("控制台输出"); 9 for (int i : a) { 10 System.out.print(i+" "); 11 } 12 } 13 14 }
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]•
1 package exp_class; 2 3 public class pra_01 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 String str[]=new String[] {"neusofteducation"}; 8 String copystr[]=new String[1]; 9 System.out.println("copystr赋值前"); 10 for (String string : copystr) { 11 System.out.println(string); 12 } 13 System.out.println("copystr赋值后"); 14 System.arraycopy(str, 0, copystr, 0, copystr.length); 15 for (int i = 0; i < copystr.length; i++) { 16 System.out.println(copystr[i]); 17 } 18 } 19 20 }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
1 package exp_class; 2 3 import java.lang.reflect.Array; 4 import java.util.Arrays; 5 6 public class sort { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 System.out.println("创建数组a,b"); 11 int a[]=new int[]{1,6,2,3,9,4,5,7,8}; 12 int b[]=new int[a.length]; 13 System.out.println("排序前"); 14 int c=0; 15 for (int j = 0; j < a.length; j++) { 16 System.out.print(a[j]+"\t"); 17 } 18 System.arraycopy(a, 0, b, 0, a.length); 19 20 for (int i = 0; i < a.length; i++) { 21 for (int j = 0; j < a.length-1; j++) { 22 if(a[j]>a[j+1]) { 23 c=a[j]; 24 a[j]=a[j+1]; 25 a[j+1]=c; 26 } 27 28 } 29 } 30 System.out.println("\n排序后"); 31 for (int i : a) { 32 System.out.print(i+"\t"); 33 } 34 System.out.println("\n对数组b用Array.sort排序"); 35 System.out.println("排序前"); 36 for (int i : b) { 37 System.out.print(i+"\t"); 38 } 39 Arrays.sort(b,0,b.length); 40 System.out.println("\n排序后"); 41 for (int i : b) { 42 System.out.print(i+"\t"); 43 } 44 } 45 46 47 }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 double a[][]=new double[5][4]; 8 a[0]=new double[]{0,1,2,3}; 9 a[1]=new double[]{4,5,6,7}; 10 a[2]=new double[]{8,9,10,11}; 11 a[3]=new double[]{12,13,14,15}; 12 a[4]=new double[]{16,17,18,19}; 13 System.out.println("控制台输出"); 14 for (int i = 0; i < a.length; i++) { 15 for (int j = 0; j < a[i].length; j++) { 16 System.out.print(a[i][j]+"\t"); 17 } 18 System.out.println(); 19 } 20 } 21 22 23 }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]•
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]=new int[]{18,25,7,36,13,2,89,63}; 8 System.out.println("控制台输出"); 9 int max=a[0]; 10 int maxindex=0; 11 for (int i = 0; i < a.length; i++) { 12 if(a[i]>max) { 13 max=a[i]; 14 maxindex=i; 15 } 16 } 17 System.out.println("max="+max+"maxindex="+maxindex); 18 19 20 } 21 22 23 }
作业
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]=new int[]{1,2,3,4,5,6,7,8,9,10}; 8 System.out.println("控制台逆序输出"); 9 for (int i = a.length-1;i>=0; i--) { 10 System.out.print(a[i]+" "); 11 } 12 } 13 } 14 15
7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业•
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]=new int[]{1,2,3,3,5,6,7,8,9,3,10,9,9,10}; 8 System.out.println("输出数组元素"); 9 for (int i = 0;i<a.length;i++) { 10 System.out.print(a[i]+" "); 11 } 12 int c=0; 13 System.out.println("重复元素保留一个其他的清零后"); 14 for (int i = 0; i < a.length; i++) { 15 for (int j = 0; j < a.length-1; j++) { 16 if(a[j]>a[j+1]) { 17 c=a[j]; 18 a[j+1]=a[j]; 19 a[j]=c; 20 } 21 if(a[j]==a[j+1]) { 22 a[j]=0; 23 } 24 } 25 } 26 for (int i = 0; i < a.length; i++) { 27 System.out.print(a[i]+" "); 28 } 29 30 } 31 32 33 } 34 35 36 37
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]=new int[]{ -10,2,3,246,-100,0,5}; 8 System.out.println("输出数组元素"); 9 10 int max=a[0]; 11 int min=a[0]; 12 double ave=0; 13 double sum=0; 14 for (int i : a) { 15 if(i>max) { 16 max=i; 17 } 18 if(i<min) { 19 min=i; 20 } 21 sum+=i; 22 } 23 ave=sum/a.length; 24 System.out.println("max="+max+" min="+min+" ave="+ave); 25 26 } 27 28 } 29 30 31 32
9、使用数组存放裴波那契数列的前20项 ,并输出
1 package exp_class; 2 3 public class pra { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int qbn[]=new int[20]; 8 int a1=0,a2=1; 9 for (int i = 0; i < qbn.length; i++) { 10 if(i==0) { 11 qbn[i]=a1; 12 } 13 if(i==1) { 14 qbn[i]=a2; 15 } 16 if(i>1) { 17 qbn[i]=qbn[i-1]+qbn[i-2]; 18 } 19 20 } 21 22 for (int i : qbn) { 23 System.out.print(i+" "); 24 } 25 26 } 27 28 } 29 30 31 32
10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出