1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package number6; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[] z={10,20,30,40,50}; for(int i=0;i) { System.out.print(z[i] + " "); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值。
package number6; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int [] a = new int[5]; a[0] = 10; a[1] = 20; a[2] = 30; a[3] = 40; a[4] = 50; for(int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值
package number6; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {23,45,22,33,56}; double average = 0; int sum =0; for(int i =0;i<5;i++) { sum+=a[i]; } System.out.println("和:"+sum+"平均值:"+sum/a.length); } }
4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
法1:
package number6; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[] { 18, 25, 7, 36, 13, 2, 89, 63 }; int max = a[0]; for (int i = 0; i < a.length; i++) { if (max < a[i]) { max = a[i]; } } System.out.println("最大值是:" + max); int count = 0; for (int i = 0; i < a.length; i++) { if (a[i] == max) { System.out.println("下标值是:" + count); } count++; } } }
法2:
package number6; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int [] a = {18,25,7,36,13,2,89,63}; int max = a[0]; int seat = 0; for(int i = 0; i < a.length; i++) { if(a[i] > max) { max = a[i]; seat = i; } } System.out.println("最大值是 :" + max + "下标值是:" + seat); } }
5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
法1:
package number6; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[] a= {18,25,7,36,13,2,89,63}; for(int i=7;i>=0;i--) { System.out.print(a[i]+ " "); } } }
法2:
package number6; import java.util.Scanner; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new java.util.Scanner(System.in); int myList[] = new int[20]; System.out.println("请输入多个正整数并以空格隔开(输入0表示结束)"); int c = 0; do { myList[c] = input.nextInt(); c++; } while (myList[c - 1] != 0); System.out.println("输入的数组为:"); for (int i = 0; i < c - 1; i++) { System.out.print(myList[i] + " "); } System.out.println(); System.out.println("输入的数组逆序输出为:"); for (int i = c - 2; i >= 0; i--) { System.out.print(myList[i] + " "); } } }
6、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。(知识点:数组遍历、数组元素访问)(不会******)
package number6; import java.util.Scanner; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = {18,25,7,36,13,2,89,63}; System.out.print("原始数组:"); for(int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } int m = arr.length; int[] arrs = new int[m+1]; Scanner input = new Scanner(System.in); System.out.println(); System.out.println("输入你要插入的整数:"); int num = input.nextInt(); for(int i = 0; i < arr.length; i++){ arrs[i] = arr[i]; //赋值 } arrs[arrs.length-1] = num; //插入新数组最后一位 int temp; for(int i = 0; i < arrs.length-1; i++){ for(int j = 0; j < arrs.length-1-i; j++){ if(arrs[j+1] < arrs[j]){ //排序 temp = arrs[j]; arrs[j] = arrs[j+1]; arrs[j+1] = temp; } } } System.out.print("插入后的并排序的新数组为:" + " "); for(int i = 0; i < arrs.length; i++){ System.out.print(arrs[i] + " "); } } }