第八周作业
1.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值
package School.Day9; public class Test01 { public static void main(String[] args) { int[] a = {10, 20, 30, 40, 50}; for (int i : a) { System.out.println(i); } } }
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
package School.Day9; public class Test02 { public static void main(String[] args){ char[] a = {'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n'}; char[] b = new char[16]; System.arraycopy(a, 0, b, 0, a.length); for (char c : b) { System.out.print(c + " "); } } }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。
package School.Day9; import java.util.Arrays; public class Test03 { public static void main(String[] args){ 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 School.Day9; public class Test04 { public static void main (String[] args){ 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] = i*10 + j; System.out.print(a[i][j] + "\t"); } System.out.println(); } } }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
package School.Day9; public class Test05 { public static void main(String[] args){ int[] a = {18,25,7,36,13,2,89,63}; int max = 0; int maxi = 0; for (int i = 0;i<a.length - 1;i++) { if (a[i] < a[i+1]) { max = a[i+1]; max = i+1; } if (a[i] > a[i+1]) { max = a[i]; maxi = i+1; } } System.out.println("此数组的最大值是: " + max); System.out.println("此数组的最大值的下标是: " + maxi); } }
6、将一个数组中的元素逆序存放
package School.Day9; import java.util.Scanner; public class Test06 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] a = new int[5]; for (int i = a.length - 1; i >= 0; i--) { System.out.print("存放元素: "); int x = s.nextInt(); a[i] = x; } System.out.print("数组元素: "); for (int i : a) { System.out.print(i + "\t"); } } }
7. 将一个数组中的重复元素保留一个其他的清零。
package School.Day9; import java.util.Scanner; public class Test07 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] a = new int[5]; for (int i = 0; i < a.length; i++) { System.out.print("存入元素: "); int x = s.nextInt(); a[i] = x; } for (int i = a.length-1; i >= 0 ; i--) { for (int j = i - 1 ; j >= 0 ; j--) { if (a[i] == a[j]) { a[i] = 0; } } } System.out.print("输出元素: "); for (int i : a) { System.out.print(i + "\t"); } } }
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。
package School.Day9; public class Test08 { public static void main(String[] args) { int[] a = {-10,2,3,246,-100,0,5}; int sum = 0; int max = 0; int sma = 0; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; } if (a[i] < sma) { sma = a[i]; } sum = sum + a[i]; } System.out.println("这个数组最大数是: " + max); System.out.println("这个数组最小数是: " + sma); System.out.println("这个数组平均数是: " + sum /7.0); } }
9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
package School.Day9; public class Test09 { public static void main(String[] args) { 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.print(i + "\t"); } } }
10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
package School.Day9; import java.util.Arrays; import java.util.Random; public class Test10 { public static void main(String[] args) { Random r = new Random (); int[] a = new int[10]; for (int i = 0;i<a.length;i++) { int x = r.nextInt(101); a[i] = x; } System.out.print("数组: "); for (int i : a) { System.out.print(i + "\t"); } Arrays.sort(a); System.out.println(); System.out.print("数组排序: "); for (int i : a) { System.out.print(i + "\t"); } } }