Java Day14

数组操作

一 代码运行中是否有需要考虑的异常情况?
比如以下例题越界问题
用户指定的下标位置,超出的有效位置

需要在代码中进行参数合法性判定!!!

参数合法性判断要放在方法内 而不能放在调用方法的位置,这也是方法实现功能的一部分(也可这样想如果你封装的方法多了 你总不能在main方法中 ,都写成合法性判断,功能多了,你把合法性判断全部写在main方法中,眼花缭乱,整体性不好。)
同时,也无需多写:这个方法该干嘛就只写实现这个功能的代码,不该它做的事情就不写进去. 比如只是查找下标就不用吧打印也写进去,具体查找到下标后的下一步就交给这个方法以外的其他代码来实现

例题

 package com.qfedu.a.homework;

public class HomeWork1 {
	public static void main(String[] args) {
		int[] array = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
		
		int maxIndex = maxIndexOf(array);
		
		System.out.println("最大值下标位置:" + maxIndex);
	}
	/*
	 * a. 找出数组中最大值的下标位置
	 * 方法分析:
	 * 		固定格式:
	 * 			public static 不要问
	 * 		返回值类型:
	 * 			int 返回值数据是类型
	 * 		方法名:
	 * 			maxIndexOf 最大值下标位置
	 * 		形式参数列表:
	 * 			int[] arr 这里需要在一个int类型数组中找出
	 * 
	 * 方法声明:
	 * 		public static int maxIndexOf(int[] arr)
	 */
	
	/**
	 * 找出数组中最大值的下标位置
	 * 
	 * @param arr 查询最大值下标位置的数组
	 * @return 返回最大值所在的下标位置,int类型
	 */
	public static int maxIndexOf(int[] arr) {
		int maxIndex = 0;
		
		for (int i = 1; i < arr.length; i++) {
			if (arr[maxIndex] < arr[i]) {
				maxIndex = i;
			}
		}
		
		return maxIndex;
	}
}

找出数组中最小值的下标位置

package com.qfedu.a.homework;

public class HomeWork2 {
	public static void main(String[] args) {
		int[] array = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
		
		int minIndex = minIndexOf(array);
		
		System.out.println(minIndex);
	}
	
	/*
	 * b. 找出数组中最小值的下标位置
	 * 方法分析:
	 * 		固定格式:
	 * 			public static
	 * 		返回值类型:
	 * 			int 
	 * 		方法名:
	 * 			minIndexOf
	 * 		形式参数列表:
	 * 			(int[] arr)
	 * 方法声明:
	 * 		public static int minIndexOf(int[] array)
	 */
	
	/**
	 * 找出数组中最小值下标位置
	 * 
	 * @param array 查询操作的数组
	 * @return 最小值的下标位置
	 */
	public static int minIndexOf(int[] array) {
		int minIndex = 0;
		
		for (int i = 1; i < array.length; i++) {
			if (array[minIndex] > array[i]) {
				minIndex = i;
			}
		}
		
		return minIndex;
	}
}

在指定位置插入指定元素
方法分析:
固定格式:
public static
返回值类型:
void:
OK选择!!!
int:
操作成功返回1,失败返回-1
boolean: [选择]
添加成功返回true,运行失败返回false
方法名:
add 这里是一个添加操作
形式参数列表:
1. 添加数据的数组
2. 指定添加的下标位置
3. 指定添加的数据
(int[] arr, int index, int insert)
方法声明:
public static boolean add(int[] arr, int index, int insert)

package com.qfedu.a.homework;

/**

  • 在数组中指定下标位置添加元素
  • @author Anonymous

*/

public class HomeWork3 {
	public static void main(String[] args) {
		int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
		
		boolean ret = add(array, 5, 100);
		
		if (ret) {
			for (int i = 0; i < array.length; i++) {
				System.out.print(array[i] + " ");
			}
			System.out.println();
		} else {
			System.out.println("方法运行失败!!!");
		}
	}
	
	/**
	 * 在数组arr中指定下标位置,添加指定元素
	 * 
	 * @param arr    添加元素是数组
	 * @param index  指定添加数据的下标位置
	 * @param insert 指定添加的数据
	 * @return 方法运行成功完成添加操作,返回true,否则返回false
	 */
	public static boolean add(int[] arr, int index, int insert) {
		// 参数合法性判断
		if (index < 0 || index > arr.length - 1) {
			System.out.println("Input Parameter is Invalid!");
			// 用户传入参数不合法,返回false,方法运行失败,终止方法运行
			return false;
			// System.exit(0) 退出整个程序
		}
		
		/*
		 * 数据是从最后一个有效元素下标位置开始整体向后移动,移动结束位置是
		 * 插入元素的下标位置。
		 * 
		 * 先完成一个大概的循环要求,然后根据推演结果完成精细加工过程
		 */
		/*
		 * arr.length - 1 ==> 9
		 * index = 5;
		 * 
		 * arr[9] = arr[9 - 1];
		 * arr[8] = arr[8 - 1];
		 * arr[7] = arr[7 - 1];
		 * arr[6] = arr[6 - 1];
		 * 
		 * for循环满足要求
		 */
		for (int i = arr.length - 1; i > index; i--) {
			arr[i] = arr[i - 1];
		}
		
		// 用户指定位置添加指定元素
		arr[index] = insert;
		
		// 运行成功返回true
		return true;
	}
}
  1. 选择排序算法推导

1 找出数组中最大值,和下标为0的元素互换位置
2 接上一题, 找出数组中剩余数据最大值,和下标为1的元素互换位置
3 接上一题, 找出数组中最大值,和下标为2的元素互换位置

选择排序算法
选择排序算法核心要求
1. 找出符合要求的数据下标位置
2. 从下标0位置开始,递增,数据交换

package com.qfedu.b.sort;

import java.util.Arrays;

/**
 * 选择排序算法
 * 
 * @author Anonymous
 *
 */
public class Demo2 {
	public static void main(String[] args) {
		int[] arr = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };

		selectSort(arr);

		System.out.println(Arrays.toString(arr));
	}

	/**
	 * 选择排序算法
	 * 
	 * @param arr int类型数组
	 */
	public static void selectSort(int[] arr) {
		// 外层循环,控制需要进行核心操作的次数,次数是数据量 - 1
		for (int i = 0; i < arr.length - 1; i++) {

			// 从 i值位置进行搜索
			int index = i;

			// 找出符合要求的极值下标位置
			for (int j = i + 1; j < arr.length; j++) {

				// if 之后是选择排序排序算法的核心
				if (arr[index] < arr[j]) {
					index = j;
				}
			}

			// 交换
			if (index != i) {
				int temp = arr[i];
				arr[i] = arr[index];
				arr[index] = temp;
			}
		}
	}

}
  1. 从推导 ==> 方法
  2. 学会总结,找出规律,善于发现规律,需要始终保存一个封装的思想!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值