1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]

 1 package work8;
 2 import java.util.Scanner;
 3 public class Exercise1 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)
10         Scanner input=new Scanner(System.in);
11         int []a=new int[5];
12         System.out.println("请给数组赋值为10,20,30,40,50");
13         for (int i = 0; i < a.length; i++) {
14             a[i]=input.nextInt();
15         }
16         for (int i = 0; i < a.length; i++) {
17             System.out.println(a[i]);
18         }
19         
20 
21     }
22 
23 }

 

2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]•

 1 package work8;
 2 
 3 public class Exercise2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) 
10         char []a=new char[]{'c','h','i','n','a'};
11         char []b=new char[5];
12         System.out.println("原数组b的值为");
13         for (int i = 0; i < b.length; i++) {
14             System.out.println(b[i]);
15         }
16         System.arraycopy(a, 0, b, 0, 5);
17         System.out.println("复制后数组b的值为");
18         for (int i = 0; i < b.length; i++) {
19             System.out.println(b[i]);
20         }
21 
22     }
23 
24 }

 

3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)

 1 package work8;
 2 
 3 public class Exercise3 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
10         int []a=new int[]{1,6,2,3,9,4,5,7,8};
11         java.util.Arrays.sort(a);
12         System.out.println("排序后的数组的值为");
13         for (int i = 0; i < a.length; i++) {
14             System.out.println(a[i]);
15         }
16 
17     }
18 
19 }

 

4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)

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

 

5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]•

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

 

作业
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•

 1 package work8;
 2 import java.util.Scanner;
 3 public class Homework1 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•
10                 Scanner input=new Scanner(System.in);
11                 int []a=new int[5];
12                 for (int i = 0; i < a.length; i++) {
13                     a[i]=input.nextInt();
14                 }
15                 System.out.println("逆序存放后的数组为");
16                 for (int i = a.length; i >0 ; i--) {
17                     System.out.println(a[i-1]);
18                 }
19 
20     }
21 
22 }

  

 

7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业•

 1 package work8;
 2 import java.util.Scanner;
 3 public class Homework2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业•
10         Scanner input=new Scanner(System.in);
11         int []a=new int[8];
12         System.out.println("请输入a数组的数据");
13         for (int i = 0; i < a.length; i++) {
14             a[i]=input.nextInt();
15         }
16         for (int i = 0; i < a.length; i++) {
17             System.out.println(a[i]);
18         }
19         System.out.println("重复元素清零后的数组a为");
20         for (int i = 0; i < a.length; i++) {
21             for (int j = 0; j < a.length; j++) {
22                 if (a[i]==a[j]&&i!=j) {
23                     a[j]=0;
24                 }
25             }
26             System.out.println(a[i]);
27         }
28 
29     }
30 
31 }

 

8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)

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

 

9、使用数组存放裴波那契数列的前20项 ,并输出

 1 package work8;
 2 
 3 public class Homework4 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 9、使用数组存放裴波那契数列的前20项 ,并输出
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-2]+a[i-1];
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 work8;
 2 import java.util.Random;
 3 public class Homework5 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // 10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
10         Random r=new Random();
11         int []a=new int[10];
12         for (int i = 0; i < a.length; i++) {
13             a[i]=r.nextInt(100);
14             System.out.println(a[i]);
15         }
16         java.util.Arrays.sort(a);
17         System.out.println("排序后");
18         for (int i = 0; i < a.length; i++) {
19             System.out.println(a[i]);
20         }
21 
22     }
23 
24 }

posted on 2021-04-24 15:45  李育博  阅读(180)  评论(0编辑  收藏  举报