JAVA 8
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
1 package G1; 2 3 public class G1 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]={10,20,30,40,50}; 8 for (int i = 0; i < a.length; i++) { 9 System.out.println(a[i]); 10 } 11 12 } 13 }
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
1 package G1; 2 3 public class G2 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 char[] a = { 'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c', 8 'a', 't', 'i', 'o', 'n' }; 9 char[] b = new char[a.length]; 10 System.arraycopy(a, 0, b, 0, a.length); 11 for (int i = 0; i < b.length; i++) { 12 System.out.print(b[i]); 13 } 14 } 15 16 }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。
1 package G1; 2 3 import java.util.Arrays; 4 5 public class G3 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 System.out.println("Arrays.sort排序"); 10 int []a={1,6,2,3,9,4,5,7,8}; 11 Arrays.sort(a); 12 for(int i : a){ 13 System.out.println(i); 14 } 15 System.out.println("冒泡排序"); 16 int[] b={1,6,2,3,9,4,5,7,8}; 17 for (int i = 0; i < b.length-1; i++) { 18 for (int j = 0; j < b.length-1-i; j++) { 19 if (b[j] > b[j + 1]) { 20 int temp = a[j]; 21 b[j] = b[j + 1]; 22 b[j + 1] = temp; 23 } 24 25 } 26 27 28 } 29 for (int i : a) { 30 System.out.println(i); 31 } 32 33 } 34 35 36 }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值
1 package G1; 2 3 public class G4 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 double [][]a=new double[5][4]; 8 for (int i = 0; i < a.length; i++) { 9 for (int j = 0; j < a[i].length; j++) { 10 a[i][j]=i+j; 11 } 12 } 13 14 System.out.println("矩阵是"); 15 16 for (int i = 0; i < a.length; i++) { 17 for (int j = 0; j < a[i].length; j++) { 18 System.out.print(a[i][j]+" "); 19 } 20 System.out.println(); 21 } 22 23 24 } 25 26 }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]•
1 package G1; 2 3 public class G5 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int []a={18,25,7,36,13,2,89,63}; 8 int max=a[0]; 9 int b=-1; 10 for (int i = 0; i < a.length; i++) { 11 if(a[i]>max){ 12 max=a[i]; 13 } 14 b++; 15 } 16 System.out.println("最大值是"+max); 17 System.out.println("下标为"+b); 18 } 19 20 21 }
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]
1 package G1; 2 3 public class G6 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] a=new int[]{1,2,3,4,5}; 8 System.out.println("原数为:"); 9 System.out.println("12345"); 10 System.out.println("逆序输出为"); 11 for (int i = a.length-1;i>=0; i--) { 12 System.out.print(a[i]); 13 } 14 } 15 16 17 }
7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问) [选作题]课后作业
1 package G1; 2 3 public class G7 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] a=new int[]{1,5,7,13,15,7,20}; 8 for (int i = 0; i < a.length; i++) { 9 for (int j = 0; j < a.length; j++) { 10 if (a[i]==a[j] && i!=j) { 11 a[j]=0; 12 } 13 } 14 } 15 for (int i : a) { 16 System.out.println(i); 17 } 18 } 19 20 21 }
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值
1 package G1; 2 3 public class G8 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int []arr={-10,2,3,246,-100,0,5}; 8 double p,sum;//平均值 9 int max,min; 10 sum=0; 11 max=arr[0]; 12 min=arr[0]; 13 for(int i=1;i<arr.length;i++){ 14 if(arr[i]>max) 15 max=arr[i]; 16 if(arr[i]<min) 17 min=arr[i]; 18 } 19 for(int k=0;k<arr.length;k++){ 20 sum+=arr[k]; 21 } 22 p=sum/7; 23 System.out.print("最大值是"+max+"\n最小值是"+min+"\n平均值是"+p); 24 } 25 26 }
9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
1 package G1; 2 3 public class G9 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int []arr=new int[20]; 8 int a,sum; 9 a=1; 10 sum=0; 11 for(int i=1;i<=arr.length;i++){ 12 sum+=a; 13 a=sum-a; 14 System.out.print(sum+"\t"); 15 } 16 } 17 18 }
10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
1 package G1; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 6 public class G10 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Random r = new Random(); 11 int x[] = new int[10]; 12 for (int i = 0; i < x.length; i++) { 13 x[i] = r.nextInt(101); 14 } 15 for (int i = 0; i < x.length; i++) { 16 System.out.print(x[i] + " "); 17 } 18 Arrays.sort(x); 19 System.out.println(); 20 for (int i = 0; i < x.length; i++) { 21 System.out.print(x[i] + " "); 22 } 23 } 24 25 }