0426数组操作

数组操作

2.数组练习

2.1 找出数组中最大值的下标位置
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;
	}
}
2.2 找出数组中最小值的下标位置
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;
	}
}
2.3 在指定位置插入指定元素【难点】
 c. 在指定位置插入指定元素【难点】
    存在一个数组,数组中的元素为
		int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
		要求
			1. 0是无效元素,仅占位使用
			2. 当前数组中【有效元素】个数为9
		需求
			在该数组中的指定下标位置放入指定元素

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

需要在代码中进行参数合法性判定!!!
方法分析:
	固定格式:
		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;
	}
}

2.4 删除数组中的指定下标的元素【难点】
d. 删除数组中的指定下标的元素【难点】
	存在一个数组,数组中的元素为
		int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
		要求:
			1. 0是无效元素,仅占位使用
		需求:
			在当前数组中删除指定下标的元素
		例如:
			删除下标5的元素
			结果 {1, 3, 5, 7, 9, 13, 15, 17, 19, 0} 
			0占位!!!

方法分析:
	固定格式:
		public static 不要问
	返回值类型:
		boolean: 检测方法方法运行的状态,运行成功返回true,运行失败返回false
	方法名:
		remove √
        delete 
    形式参数列表:
    	1. int[] arr 需要删除数据的数组
    	2. int index 指定删除的下标位置
    		这里需要对于下标位置进行判断,必须是一个合法的下标位置
    	(int[] arr, int index)
方法声明:
	public static boolean remove(int[] arr, int index)
package com.qfedu.a.homework;

public class HomeWork4 {
	public static void main(String[] args) {
		int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
		
		boolean ret = remove(array, 5);
		
		if (ret) {
			for (int i = 0; i < array.length; i++) {
				System.out.print(array[i] + " ");
			}
			System.out.println();
		} else {
			System.out.println("方法运行失败!!!");
		}
	}
	
	/**
	 * 删除数组中指定下标位置的元素
	 * 
	 * @param arr 需要进行参数操作的数组
	 * @param index 指定删除的下标位置
	 * @return 方法运行成功返回true,否则返回false
	 */
	public static boolean remove(int[] arr, int index) {
		// 参数合法性判断
		if (index < 0 || index > arr.length - 1) {
			System.out.println("Input Parameter is Invalid!");
			
			// 参数不合法,方法运行失败!!!
			return false;
		}
		
		/*
		 * 删除操作就是从删除位置开始,到最大下标结束
		 * 
		 * index = 5;
		 * arr.length = 10;
		 * 
		 * arr[5] = arr[5 + 1];
		 * arr[6] = arr[6 + 1];
		 * arr[7] = arr[7 + 1];
		 * arr[8] = arr[8 + 1];
		 * 
		 * arr[9] = arr[9 + 1]; 错误
		 * 所以 arr.length - 1 作为终止条件
		 */
		for (int i = index; i < arr.length - 1; i++) {
			arr[i] = arr[i + 1];
		}
		
		// 最后一个赋值为0,占位符
		arr[arr.length - 1] = 0;
		
		return true;
	}
}

3. 选择排序算法推导

3.1 找出数组中最大值,和下标为0的元素互换位置
int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
/*                                          
 * 1. 找出数组中的最大值下标位置                         
 */                                         
int index = 0;                              
for (int i = 1; i < arr.length; i++) {      
	if (arr[index] < arr[i]) {              
		index = i;                          
	}                                       
}                                           
                                            
/*                                          
 * 2. 和下标为0的元素交换位置                          
 * 如果说下标为0的元素就是最大值,这里不需要交换                  
 */                                         
if (index != 0) {                           
	int temp = arr[0];                      
	arr[0] = arr[index];                    
	arr[index] = temp;                      
}                                           
3.2 接上一题, 找出数组中剩余数据最大值,和下标为1的元素互换位置
int index = 1;                                       
/*                                               
 * 1. 从下标为1的位置开始找出最大值的下标位置                       
 */                                              
for (int i = 2; i < arr.length; i++) {           
	if (arr[index] < arr[i]) {                   
		index = i;                               
	}                                            
}                                                
                                                 
/*                                               
 * 2. 交换位置                                       
 */                                              
if (index != 1) {                                
	int temp = arr[1];                           
	arr[1] = arr[index];                         
	arr[index] = temp;                           
}                                                
3.3 接上一题, 找出数组中最大值,和下标为2的元素互换位置
int index = 2;                                       
/*                                               
 * 1. 从下标为1的位置开始找出最大值的下标位置                       
 */                                              
for (int i = 3; i < arr.length; i++) {           
	if (arr[index] < arr[i]) {                   
		index = i;                               
	}                                            
}                                                
                                                 
/*                                               
 * 2. 交换位置                                       
 */                                              
if (index != 2) {                                
	int temp = arr[2];                           
	arr[2] = arr[index];                         
	arr[index] = temp;                           
}                                                
3.4 完成选择排序算法
选择排序算法核心要求
	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;
			}
		}
	}

}
3.5 总结
1. 从推导 ==> 方法
2. 学会总结,找出规律,善于发现规律,需要始终保存一个封装的思想!!!
原文地址:https://www.cnblogs.com/raising/p/12783144.html