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

2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]•
1 package test; 2 3 public class Lwk29 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 char[] a = new char[] { 'n', 'e', 'u', 's', 'o' }; 11 char[] b = new char[a.length]; 12 System.arraycopy(a, 0, b, 0, a.length); 13 for (char c : b) { 14 System.out.print(c); 15 } 16 } 17 18 }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
1 package test; 2 3 import java.util.Arrays; 4 5 public class Lwk30 { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 // Arrays.sort排序 13 int[] a = new int[] { 1, 6, 2, 3, 9, 4, 5, 7, 8 }; 14 Arrays.sort(a); 15 for (int i : a) { 16 System.out.print(i + " "); 17 } 18 System.out.println(); 19 // 冒泡排序 20 int[] b = new int[] { 1, 6, 2, 3, 9, 4, 5, 7, 8 }; 21 for (int i = 0; i < b.length - 1; i++) { 22 for (int j = 0; j < b.length - 1 - i; j++) { 23 if (b[j] > b[j + 1]) { 24 int c = b[j]; 25 b[j] = b[j + 1]; 26 b[j + 1] = c; 27 } 28 } 29 } 30 for (int i : b) { 31 System.out.print(i + " "); 32 } 33 } 34 35 }

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

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

作业
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•
1 package test; 2 3 public class Lwk33 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 11 for (int i = a.length - 1; i >= 0; i--) { 12 System.out.println(a[i]); 13 } 14 } 15 16 }

7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业•
1 package test; 2 3 public class Lwk34 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] a = { 1, 5, 4, 5, 3, 2, 1 }; 11 for (int i = 0; i < a.length; i++) { 12 int k = 0; 13 for (int j = 0; j < a.length; j++) { 14 if (a[i] == a[j] && i != j) { 15 k = 1; 16 } 17 } 18 if (k == 0) { 19 a[i] = 0; 20 } 21 } 22 for (int i : a) { 23 System.out.print(i + " "); 24 } 25 } 26 27 }

8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
1 package test; 2 3 public class Lwk35 { 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 sum=0; 12 int max=a[0],min=a[0]; 13 for (int i = 0; i < a.length; i++) { 14 sum+=a[i]; 15 if (max<a[i]) { 16 max=a[i]; 17 } 18 if (min>a[i]) { 19 min=a[i]; 20 } 21 } 22 System.out.println("平均值为"+(int)(sum/a.length)+"最大值为"+max+"最小值为"+min); 23 } 24 25 }

9、使用数组存放裴波那契数列的前20项 ,并输出
1 package test; 2 3 public class Lwk36 { 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 : a) { 17 System.out.print(i+" "); 18 } 19 } 20 21 }

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

浙公网安备 33010602011771号