Java_SE数组练习

数组练习

public static void main(String[] args) {
		// TODO Auto-generated method stub

		//数组中的最大值,最小值,数组值的和
		exercise_01();
		
		//数组中第一个指定数值的下标
		exercise_02();
		
		//数组中最大值和其下标
		exercise_03();
		
		//去除数组中最大值和最小值的平均数
		exercise_04();
		
		//数组中的最小值
		exercise_05();
		
		//数组的乘积
		exercise_06();
		
		//数组中奇数的乘积
		exercise_07();
		
		//数组中指定字符出现的次数
		exercise_08();
		
		//将arr数组中的9,96,9,6 到新数组newArr中
		exercise_09();
		
		//数组反转
		exercise_10();
		
		//数组去除0放进新数组
		exercise_11();
		
		//数组合并排序
		exercise_12();
	}

	/**
	 * 数组中的最大值,最小值,数组值的和
	 */
	 public static void exercise_01(){
		int[] arr = {124,532,52,64,5,547,43,43,747};
		System.out.println();
		System.out.println("----------------------------1---------------------------");
		int sum = 0;
		int max = arr[0];
		int min = arr[0];
		
		for (int i : arr) {
			sum += i;
			if (max < i) {
				max = i;
			}
			if (min > i) {
				min = i;
			}
		}
		System.out.println("数组和是:"+sum);
		System.out.println("数组中的最大值:"+max);
		System.out.println("数组中的最小值:"+min);
	}
	
	/**
	 * 数组中第一个指定数值的下标
	 */
	 public static void exercise_02(){
		int[] arr = {34,63,25,76,22,54,65,22};
		int key = 9;
		System.out.println();
		System.out.println("----------------------------2---------------------------");
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == key) {
				System.out.println(key+"第一次下标是:"+i);
				break;
			}
			if (i == arr.length-1) {
				System.out.println("数组中没有该数值!");
			}
		}
		
	}
	
	/**
	 * 数组中最大值和其下标
	 */
	 public static void exercise_03(){
		int[] arr = {212,352,535,658,321,972,221,58,685,452}; 
		int max = arr[0];
		int index  = 0;
		System.out.println();
		System.out.println("----------------------------3---------------------------");
		for (int i = 0; i < arr.length; i++) {
			if (max < arr[i]) {
				max = arr[i];
				index = i;
			}
		}
		System.out.println("数组最大值:"+max+" 下标是:"+index);
	}
	
	/**
	 * 去除数组中最大值和最小值的平均数
	 */
	 public static void exercise_04(){
		int[] arr = {7,4,8,9,10,7,6,9,10,8};
		int max = arr[0];
		int min = arr[0];
		int sum = 0;
		System.out.println();
		System.out.println("----------------------------4---------------------------");
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
			if (max < arr[i]) {
				max = arr[i];
			}
			if (min > arr[i]) {
				min = arr[i];
			}
		}
		System.out.println("平均分数:");
		System.out.println((sum-max-min)/10);
	}
	
	/**
	 * 数组中的最小值
	 */
	 public static void exercise_05(){
		int[] arr = {96,9,1,9,6,66};
		int min = arr[0];
		System.out.println();
		System.out.println("----------------------------5---------------------------");
		for (int i : arr) {
			if (min > i) {
				min = i;
			}
		}
		System.out.println("数组中最小值:"+min);
	}
	
	/**
	 * 数组的乘积
	 */
	 public static void exercise_06(){
		int[] arr = {1,9,96,9,6,66};
		int product = 1;
		System.out.println();
		System.out.println("----------------------------6---------------------------");
		for (int i : arr) {
			product *= i;
		}
		System.out.println("数组的乘积:"+product);
	}
	
	/**
	 * 数组中奇数的乘积
	 */
	 public static void exercise_07(){
		int[] arr = {1,2,3,4,5,6,7,8,9,11,12};
		int sum = 0;
		System.out.println();
		System.out.println("----------------------------7---------------------------");
		for (int i : arr) {
			if (i % 2 != 0) {
				sum += i;
			}
		}
		System.out.println(sum);
	}
	
	/**
	 * 数组中指定字符出现的次数
	 */
	 public static void exercise_08(){
		char[] c = {'a','f','j','j','a','e','g'};
		int count = 0;
		char key = 'a'; 
		System.out.println();
		System.out.println("----------------------------8---------------------------");
		for (char d : c) {
			if (key == d) {
				count++;
			}
		}
		System.out.println(key+" 在数组中出现了:"+count);
	}
	
	/**
	 * 将arr数组中的9,96,9,6 到新数组newArr中
	 */
	static public void exercise_09(){
		int[] arr = {1,9,96,9,6,66};
		int[] newArr = new int[4];
		
		System.out.println();
		System.out.println("----------------------------9---------------------------");
		for (int i = 0; i < newArr.length; i++) {
			newArr[i] = arr[i+1];
			System.out.print(newArr[i]+" ");
		}
	}
	
	/**
	 * 数组反转
	 */
	 public static void exercise_10(){
		 int[] arr = {23,43,65,77,13,32,5,7,45};
		 System.out.println();
		 System.out.println("----------------------------10---------------------------");

		for (int i = 0; i < arr.length/2; i++) {
				int tmp = arr[i];
				arr[i] = arr[arr.length-1-i];
				arr[arr.length-1-i] = tmp;
		}
		
		for (int i : arr) {
			System.out.println(i);
		}
		System.out.println();
	}
	
	/**
	 * 数组去除0放进新数组
	 */
	
	 public static void exercise_11(){
		int[] oldArr = {1,3,4,5,6,6,0,5,6,7,0,5,34,46,0,4};
		int zero = 0;//计数旧数组有多少个0
		System.out.println();
		System.out.println("----------------------------11---------------------------");
		//计数旧数组有多少个0
		for (int i = 0; i < oldArr.length; i++) {
			if (oldArr[i] == 0) {
				zero++;
			}
		}
		
		//旧数组去除0后的新数组
		int[] newArr = new int[oldArr.length-zero];
		
		//新数组赋值
		int j = 0;
		for (int i = 0; i < oldArr.length; i++) {
			if (oldArr[i] != 0) {
				newArr[j] = oldArr[i];
				j++;
			}
		}
		
		//输出新数组的值
		System.out.println("新数组:");
		for (int i : newArr) {
			System.out.print(i+" ");
		}
	}
	
	/**
	 * 数组合并排序
	 */
	 public static void exercise_12(){
		
		System.out.println();
		System.out.println("----------------------------12---------------------------");
		
		//数组合并排序
		int[] arr1 = {1,7,9,11,13,15,17,19};
		int[] arr2 = {2,4,6,8,10};
		int[] arr = new int[arr1.length+arr2.length];
		int tmp = 0;
		
		//数组拼接arr1的值
		for (int i = 0; i < arr1.length; i++) {
			arr[i] = arr1[i];
		}
		//数组拼接arr2的值
		for (int i = 0; i < arr2.length; i++) {
			arr[arr1.length+i] = arr2[i];
		}
		System.out.println("数组合并:");
		for (int i : arr) {
			System.out.print(i+" ");
		}
		
		//拼接后数组排序
		System.out.println();
		System.out.println("排序:");
		for (int i = 0; i < arr.length-1; i++) {
			for (int j = 0; j < arr.length-1-i; j++) {
				if (arr[j]>arr[j + 1]) {
					tmp = arr[j];	
					arr[j] = arr[j+1];
					arr[j+1] = tmp;	
				}
			}
		}
		for (int i : arr) {
			System.out.print(i+" ");
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值