Java第八次作业
1.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。
packagesjlx; publicclass jslx1 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int x[]=newint[]{10,20,30,40,50}; for (inti=0;i<x.length;i++){ System.out.println(x[i]); } } }
2、将一个字符数组的值(neusofte)拷贝到另一个字符数组中。(知识点:数组复制)
packagesjlx; publicclass sjlx2 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub char x[]=newchar[]{'n','e','u','s','o','f','t','e'}; char y[]=newchar[x.length]; System.arraycopy(x, 0, y, 0, x.length); for (char c : y) { System.out.print(c); } } }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。
packagesjlx; importjava.util.Arrays; publicclass sjlx4 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int x[]=newint[]{1,6,2,3,9,4,5,7,8}; for (int i=0;i<x.length-1;i++){ for (int j=0;j<x.length-i-1;j++){ if(x[j]>x[j+1]){ int temp=x[j]; x[j]=x[j+1]; x[j+1]=temp; } } } Arrays.sort(x); for (int n:x){ System.out.println(n); } } }
4、输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
packagesjlx; publicclass sjlx5 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub double a[][] = newdouble[][] { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9 } }; 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)的数组中找出其中最大的数及其下标。
packagesjlx; publicclass sjlx6 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int x[]=newint[]{18,25,7,36,13,2,89,63}; int max=x[0]; for (int i = 0; i <x.length; i++) { if(max<x[i]){ max=x[i]; } } System.out.println("最大值是"+max); int count = 0; for (int i = 0; i <x.length; i++) { if(x[i]==max){ System.out.println("下标值是"+count); } count++; } } }
作业
1.将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
package exe2; public class text7 { public static void main(String[] args) { // TODO Auto-generated method stub int x[] = { 1, 2, 3, 4, 5 }; int y = 0; for (int i = 0; i < x.length / 2; i++) { y = x[i]; x[i] = x[x.length - 1 - i]; x[x.length - 1 - i] = y; } System.out.println("逆序存放为:"); for (int i : x) { System.out.print(i); } } }
2、 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问
package exe2; public class text7 { public static void main(String[] args) { // TODO Auto-generated method stub int x[] = {1,2,3,4,5,6,5,4,3 }; for (int i = 0; i < x.length; i++) { for (int j = i + 1; j < x.length; j++) { if (x[i] == x[j]) x[j] = 0; } } for (int i = 0; i < x.length; i++) { System.out.print(x[i]+" "); } } }
3、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
package exe2; public class text7 { public static void main(String[] args) { // TODO Auto-generated method stub int[] x = { -10, 2, 3, 246, -100, 0, 5 }; int sum = 0; int max = x[0]; int min = x[0]; int b = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; if (x[i] > max) max = x[i]; if (x[i] < min) min = x[i]; } b = sum / 7; System.out.println("最大值:" + max + " 最小值:" + min + " 平均值:" + b); } }
4、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
package exe2; public class text7 { 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-2]+a[i-1]; } System.out.println("裴波那契数列的前8项是:"); for(int i=0;i<8;i++){ System.out.print(a[i]+" "); } } }
5、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
package exe2; import java.util.Arrays; import java.util.Random; public class text7 { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[10]; Random x = new Random(); for (int i = 0; i < a.length; i++) { a[i] = x.nextInt(101); } System.out.println("该数组为: "); for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } System.out.println(); System.out.println("排序后: "); Arrays.sort(a); for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } } }