4.23第八次Java作业
上机
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]•
package yxf135; import java.util.Scanner; public class sj1 { /** * @param args */ public static void main(String[] args) { // 编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]? int a[]={10,20,30,40,50}; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]•
package yxf135; public class sj2 { /** * @param args */ public static void main(String[] args) { //2、 将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]? char[] a=new char[]{'g','o','o','d'}; char[] b=new char[4]; System.arraycopy(a,0,b,0,4); for (char c : b) { System.out.println(c); } } }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
冒泡排序
package yxf135; public class sj3 { /** * @param args */ public static void main(String[] args) { // 3、 给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序) int a[]={1,6,2,3,9,4,5,7,8}; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length-1; j++) { if (a[j]>a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } for (int i : a) { System.out.println(i); } } }
JDK自带排序
package yxf135; import java.util.Arrays; public class sj3 { /** * @param args */ public static void main(String[] args) { // 3、 给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序) int a[]={1,6,2,3,9,4,5,7,8}; Arrays.sort(a); for (int i : a) { System.out.println(i); } } }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
package yxf135; public class sj4 { /** * @param args */ public static void main(String[] args) { // 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历) double [][]a=new double[5][4]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j]=j; } } System.out.println("矩阵是"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]•
package yxf135; public class sj5 { /** * @param args */ public static void main(String[] args) { // 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]? int a[]={18,25,7,36,13,2,89,63}; int max=a[0]; int maxidx=0; for (int i = 0; i < a.length; i++) { if (a[i]>max) { max=a[i]; maxidx=i; } } System.out.println("最大值"+max+"它的下标数为"+maxidx); } }
作业
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•
package array; import java.util.Scanner; public class array1 { public static void main(String[] args) { // 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) Scanner input=new Scanner(System.in); System.out.println("请输入数组中元素个数:" ); int x=input.nextInt(); int a[]=new int[x]; System.out.println("请输入数组元素:" ); for (int i = 0; i < x; i++) { a[i]=input.nextInt(); } System.out.println("逆序存放:" ); for (int j = x-1; j > 0; j--) { System.out.print(a[j]); } System.out.println(a[0]); } }
7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业•
package array; import java.util.Scanner; public class array2 { public static void main(String[] args) { // 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) Scanner input=new Scanner(System.in); System.out.println("请输入数组中元素个数" ); int x=input.nextInt(); int a[]=new int[x]; System.out.println("请输入数组元素" ); for (int i = 0; i < x; i++) { a[i]=input.nextInt(); } System.out.println("整理后" ); for (int b = 0; b < a.length; b++) { for (int n = b+1; n < a.length; n++) { if(a[b]==a[n]) { a[n]=0; } } System.out.print(a[b] ); } } }
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
package array; import java.util.Arrays; public class array3 { public static void main(String[] args) { // 给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问) int []a={ -10,2,3,246,-100,0,5}; Arrays.sort(a); System.out.println("最大值"+a[a.length-1]); System.out.println("最小值"+a[0]); double sum=0.0; for(int i=0;i<a.length;i++) { sum+=a[i]; } System.out.println("数组的平均值为"+sum/a.length); } }
9、使用数组存放裴波那契数列的前20项 ,并输出
package array; public class array4 { public static void main(String[] args) { // 使用数组存放裴波那契数列的前20项 ,并输出 int[] a= new int[20]; a[0]=1; a[1]=1; for (int i = 2; i < a.length; i++) { a[i] = a[i-1] + a[i-2]; } for (int i : a) { System.out.println(i); } } }
10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
package array; import java.util.Arrays; import java.util.Random; public class array5 { public static void main(String[] args) { // 生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出 int a[]=new int[10]; Random r=new Random(); for (int i = 0; i < a.length; i++) { a[i]=r.nextInt(101); } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } System.out.println("排序后:"); Arrays.sort(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }