Java第八次作业
- 编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
1 package tegf.gfg.trdyt; 2 3 public class test1 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] arr=new int[]{10,20,30,40,50}; 11 for(int i=0;i<arr.length;i++){ 12 System.out.print(arr[i]+" "); 13 } 14 15 } 16 17 }

2.将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
1 package tegf.gfg.trdyt; 2 3 public class test2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 String[] arr={"neusofteducation"}; 11 String[] brr=new String[1]; 12 for(int i=0;i<arr.length;i++){ 13 brr[i]=arr[i]; 14 System.out.print(brr[i]+" "); 15 } 16 } 17 18 }

3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
1 package tegf.gfg.trdyt; 2 3 import java.util.Arrays; 4 5 public class test3 { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 int[] a={1,6,2,3,9,4,5,7,8}; 13 Arrays.sort(a); 14 for(int n:a){ 15 System.out.print(n+" "); 16 } 17 } 18 19 }

1 package tegf.gfg.trdyt; 2 3 public class test4 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] a={1,6,2,3,9,4,5,7,8}; 11 int temp; 12 for(int i=0;i<a.length-1;i++){ 13 for(int j=0;j<a.length-i-1;j++){ 14 if(a[j]>a[j+1]){ 15 temp=a[j]; 16 a[j]=a[j+1]; 17 a[j+1]=temp; 18 } 19 } 20 } 21 for(int n:a){ 22 System.out.print(n+" "); 23 } 24 } 25 26 }

4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
1 package tegf.gfg.trdyt; 2 3 public class test6 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 double[][]a2=new double[5][4]; 11 for(int i=0;i<a2.length;i++){ 12 for(int j=0;j<a2[i].length;j++){ 13 a2[i][j]=i+j; 14 } 15 } 16 for(int i=0;i<a2.length;i++){ 17 for(int j=0;j<a2[i].length;j++){ 18 System.out.print(a2[i][j]+" "); 19 } 20 System.out.println(); 21 } 22 } 23 24 }

5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
1 package tegf.gfg.trdyt; 2 3 public class test5 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 10 // TODO Auto-generated method stub 11 int[] a3={18,25,7,36,13,2,89,63}; 12 int max=a3[0],maxid=0; 13 for(int i=1;i<a3.length;i++){ 14 if(max<a3[i]){ 15 max=a3[i]; 16 maxid=i; 17 } 18 } 19 System.out.println("最大数为:"+max+",下标为:"+maxid); 20 } 21 22 }

作业
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
1 package tegf.gfg.trdyt; 2 3 public class test7 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] a4={1,2,3,4,5,6,7}; 11 int temp; 12 for(int i=0;i<a4.length/2;i++){ 13 temp=a4[i]; 14 a4[i]=a4[a4.length-i-1]; 15 a4[a4.length-i-1]=temp; 16 } 17 for(int i:a4){ 18 System.out.print(i+" "); 19 20 } 21 System.out.println(); 22 } 23 24 }

7.将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
1 package fg.eds.drfg; 2 3 public class test1 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int [] a={1,2,2,3}; 11 for (int i = 0; i < a.length; i++) { 12 for (int j = 0; j < a.length; j++) { 13 if (a[i]==a[j]&&i!=j) //这里注意千万不要让元素比较自身,否则都是零了 14 { 15 a[j]=0; 16 } 17 } 18 } 19 for (int i : a) { 20 System.out.println(i); 21 } 22 23 } 24 25 }

8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
1 package fg.eds.drfg; 2 3 public class test2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int a[] = { -10, 2, 3, 246, -100, 0, 5 }; 11 double average = 0; 12 double sum = 0; 13 for (int i = 0; i < a.length; i++) { 14 sum += a[i]; 15 } 16 average = sum / a.length; 17 int max = a[0]; 18 int min = a[0]; 19 for (int i = 1; i < a.length; i++) { 20 if (a[i] > max) { 21 max = a[i]; 22 } 23 if (a[i] < min) { 24 min = a[i]; 25 } 26 } 27 System.out.println("平均数是" + average + "\n" + "最大值是" + max + "\n" 28 + "最小值是" + min); 29 } 30 31 }

9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
1 package fg.eds.drfg; 2 3 public class test3 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int a[]=new int[20]; 11 a[0]=1; 12 a[1]=1; 13 for (int i = 2; i < a.length; i++) { 14 a[i]=a[i-1]+a[i-2]; 15 } 16 for (int i = 0; i < a.length; i++) { 17 System.out.println(a[i]); 18 } 19 } 20 21 }

10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
1 package fg.eds.drfg; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 6 public class test4 { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 int a[]=new int[10]; 14 Random r=new Random(); 15 for (int i = 0; i < a.length; i++) { 16 a[i]=r.nextInt(101); 17 } 18 for (int i = 0; i < a.length; i++) { 19 System.out.println(a[i]); 20 } 21 System.out.println("---------排序后--------"); 22 Arrays.sort(a); 23 for (int i = 0; i < a.length; i++) { 24 System.out.println(a[i]); 25 } 26 27 } 28 29 }

浙公网安备 33010602011771号