第八次Java上机作业
1.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
1 package test; 2 3 public class TestArray1 { 4 // 1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]? 5 public static void main(String[] args) { 6 int a[] = { 10, 20, 30, 40, 50 }; 7 for (int i = 0; i < a.length; i++) { 8 System.out.println(a[i]); 9 } 10 } 11 }
2.将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
1 package test; 2 3 public class TestArray2 { 4 5 /** 6 * 2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]? 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int a[] = { 12, 23, 34, 45 }; 11 int b[] = { 56, 67, 78, 89, 0, 0, 0, 0, 0 }; 12 System.arraycopy(a, 0, b, 3, 3); 13 for (int i = 0; i < b.length; i++) { 14 System.out.println(b[i]); 15 } 16 17 } 18 19 }
3.给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)、
方法一
1 package test; 2 3 public class TestArray3_1 { 4 5 /** 6 * 3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序, 7 * 然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序) 8 */ 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 int a[]={1,6,2,3,9,4,5,7,8}; 12 for (int i = 0; i < a.length; i++) { 13 for (int j = 0; j < a.length-1; j++) { 14 if(a[j]>a[j+1]){ 15 int mup=0; 16 mup=a[j]; 17 a[j]=a[j+1]; 18 a[j+1]=mup; 19 } 20 } 21 22 } 23 for (int i = 0; i < a.length; i++) { 24 System.out.println(a[i]); 25 } 26 27 } 28 29 }
方法二
1 package test; 2 3 import java.util.Arrays; 4 5 public class TestArray3_2 { 6 public static void main(String[] args) { 7 int a[] = { 1, 6, 2, 3, 9, 4, 5, 7, 8 }; 8 Arrays.sort(a); 9 for (int i = 0; i < a.length; i++) { 10 System.out.println(a[i]); 11 12 } 13 14 } 15 }
4.输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
1 package bbb; 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=new int[5][4]; 11 for (int i = 0; i < a.length; i++) { 12 for (int j = 0; j < a[i].length; j++) { 13 a[i][j]=i+j; 14 } 15 } 16 17 System.out.println("矩阵是"); 18 19 for (int i = 0; i < a.length; i++) { 20 for (int j = 0; j < a[i].length; j++) { 21 System.out.print(a[i][j]+" "); 22 } 23 System.out.println(); 24 } 25 26 } 27 28 29 }
5.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
1 package bbb; 2 3 public class test5 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int a[]={18,25,7,6,13,2,89,63}; 11 int max=a[0],maxXB=0; 12 for(int i=1;i<a.length;i++){ 13 if(a[i]>max) 14 max=a[i]; 15 maxXB=i; 16 } 17 System.out.println("最大值为"+max+",下标为"+maxXB); 18 } 19 20 }
6.将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
1 package 第八周作业; 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 int num[]={2021,4,23}; 11 for (int i = 0; i < num.length/2; i++) { 12 int temp; 13 temp=num[i]; 14 num[i]=num[num.length-1-i]; 15 num[num.length-1-i]=temp; 16 } 17 for (int i = 0; i < num.length; i++) { 18 System.out.println(num[i]); 19 } 20 21 } 22 23 }
7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
1 package 第八周作业; 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[] a = {4,5,6,7,8,8,9,9, }; 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 a[j] = 0; 15 } 16 } 17 } 18 for (int i = 0; i < a.length; i++) { 19 System.out.println(a[i]); 20 } 21 22 } 23 24 }
8.给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
1 package 第八周作业; 2 3 public class Test8 { 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 32 }
9. 使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
1 package 第八周作业; 2 3 public class Test9 { 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 22 }
10.生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
1 package 第八周作业; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 6 public class Test10 { 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 }