第八周作业
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]•
package com.wyn; public class HomeWork { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int shuzu[] = new int[]{10,20,30,40,50}; for (int i : shuzu) { System.out.println(i); } } }
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]•
package com.wyn; public class HomeWork2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char[] shuzu = new char[]{'n','e','u','s','o','f','t','e','d','u','c','a','t','i','o','n'}; char[] copy = new char[16]; System.arraycopy(shuzu, 0, copy, 0, shuzu.length); for (char c : copy) { System.out.println(c); } } }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
package com.wyn; import java.util.Arrays; public class HomeWork3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int shuzu[] = new int[]{1,6,2,3,9,4,5,7,8}; Arrays.sort(shuzu); for (int i : shuzu) { System.out.println(i); } } }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
package com.wyn; import java.util.Arrays; public class HomeWork4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double shuzu[][] = new double[][]{{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}}; for(int i = 0; i<5 ; i++){ for(int j = 0 ; j<4 ; j++){ System.out.println(shuzu[i][j]); } } } }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]•
package com.wyn; public class HomeWork5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int shuzu[] = new int[]{18,25,7,36,13,2,89,63}; int maxid=0; int c=0; int max = 0; for (int i : shuzu) { if(i>max){ max=i; maxid=c; } c++; } System.out.println("zuidazhi"+max); System.out.println("xiabiao"+maxid); } }
作业
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•
package homework; public class HomeWork2 { public static void main(String[] args) { // TODO Auto-generated method stub int a[]= new int[] {1,2,3,4,5}; int temp; for(int i=0;i<a.length/2;i++){ temp=a[i]; a[i]=a[a.length-i-1]; a[a.length-i-1]=temp; } for (int i : a) { System.out.println(i); } } }
7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业•
package homework; public class HomeWork2 { public static void main(String[] args) { // TODO Auto-generated method stub int a[]= new int[] {1,2,3,4,5,2,3,2,5,3,4,5,3,2,2,4}; for(int i = 0;i<a.length;i++) { for(int j = i+1 ; j<a.length ; j++) { if(a[i]==a[j]) { a[j]=0; } } } for (int i : a) { System.out.println(i); } } }
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
public class HomeWork2 { public static void main(String[] args) { // TODO Auto-generated method stub double a[]= new double[] {-10,2,3,246,-100,0,5}; double max=a[0]; double pingjun=0; double sum=0; double min=a[0]; for(int i = 0;i<a.length;i++) { if(max<a[i]) { max=a[i]; } if(min>a[i]) { min=a[i]; } sum=sum+a[i]; } pingjun=sum/a.length; System.out.println(pingjun); System.out.println(max); System.out.println(min); } }
9、使用数组存放裴波那契数列的前20项 ,并输出
package homework; public class HomeWork2 { public static void main(String[] args) { // TODO Auto-generated method stub 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 homework; import java.util.Arrays; import java.util.Random; public class HomeWork2 { public static void main(String[] args) { // TODO Auto-generated method stub Random r = new Random(); int a[]=new int[10]; for(int i = 0;i<a.length;i++) { a[i] = r.nextInt(101); } for (int i : a) { System.out.println(i); } for(int i=0;i<a.length;i++) { } Arrays.sort(a); for (int i : a) { System.out.println(i); } } }