Java 数组综合应用

Java 数组综合应用

需求

  1. 已知有个升序的数组,要求插入一个元素,该数组顺序依然是升序,
  2. 随机生成10个1-100整数保存到数组,并降序打印及求平均值、最大值和最大值下标、并查找里面是否有 8 如果有返回下标

解决方案

需求: 已知有个升序的数组,要求插入一个元素,该数组顺序依然是升序,

public class ArrayExercise {
	public static void main (String[] args) {
		// 1. 已知有个升序的数组,要求插入一个元素,该数组顺序依然是升序,
		/*
		思路分析:
			1. 找出插入位置,
				1. 使用二分查找法,找到要插入的位置,也就是二分查找的最后一个位置
				2. 考虑特殊情况, 如 插入的数据是这个数组中最大的
			2. 创建交换数组 拷贝原数组的值,将要插入的数据插入到 指定的位置		
				1. 遍历交换数组
				2. 使用一个指针作为原数组的拷贝指针
				3. 在遍历时,如果 交换数组的指针 != 要插入的位置,则拷贝原数组数据后, 拷贝指针 +1,
					如果等于要插入的位置,则插入要插入的数据.
			3. 将插入后的数组赋给 原数组
		*/
		
		// 升序数组
		int ascendingArray[] = {-100, 2, 4, 5, 8, 19, 20, 65};
		// 打印数组内容
		System.out.print("原数组:\t");
		for (int index = 0; index < ascendingArray.length; index++) {
			System.out.print(ascendingArray[index] + " ");
		}
		System.out.println();

		// 要插入的值
		int insertValue = 65;

		// 使用二分查找法寻找要插入的位置
		int start = 0; // 起始索引
		int end = ascendingArray.length - 1;  // 结束索引
		int mid = (ascendingArray.length - 1) / 2;  // 中间索引
		// 记录查找结果
		int insertIndex;
		while (true) {
			// 如果数组中间指针指向的数等于要插入的数, 直接返回中间值
			// 如果 起始指针 中间指针 结束指针 都一致 时 判断这个指针指向的值是否小于要插入的值,如果是: 返回插入位置 中间值+1  如果不是 返回中间值
			if (ascendingArray[mid] == insertValue || start == end) {
				if (ascendingArray[mid] < insertValue) {
					// 这一步是处理特殊情况的,
					// 后面拷贝元素时直接将要插入的元素插入到 这个位置, 所以要插入的元素 必须比插入位置指向的值小					
					insertIndex = mid + 1;
				} 
				else {insertIndex = mid;}
				// 找到插入位置 结束循环
				break;

			}
			// 如果 中间指针 指向的值比要插入的小
			else if (ascendingArray[mid] < insertValue) {
				start = mid + 1;
				mid = (start + end) / 2;
			}
			// 上面所有情况都处理完了, 只剩下 中间指针指向的值比要插入的值大
			else {
				end = mid - 1;
				mid = (start + end) / 2;
			}
			
		}


		// 创建交换数组, 用于拷贝原数组和插入新元素
		int swapArray[] = new int[ascendingArray.length + 1];
		// 拷贝原数组指针
		int copyPointer = 0;
		// 遍历交换数组, 拷贝数据将要插入的元素插入到插入位置再继续进行拷贝
		for (int index = 0; index < swapArray.length; index++) {
			// 判断当前索引是否等于要插入的位置
			if (index != insertIndex) {  
				// 不是要插入的位置, 拷贝值
				swapArray[index] = ascendingArray[copyPointer];
				// 拷贝完成,拷贝指针 +1
				copyPointer++;
			
			} 
			// 如果时要插入的位置,则插入要插入的元素			
			else {swapArray[index] = insertValue;}

		}
		// 将交换数组赋给原数组
		ascendingArray = swapArray;
		
		// 查看插入后的数组
		System.out.print("插入元素: " + insertValue + "\n插入后:\t");
		for (int index = 0; index < ascendingArray.length; index++) {
			System.out.print(ascendingArray[index] + " ");
		}
		System.out.println();

	}
}

需求: 随机生成10个1-100整数保存到数组,并降序打印及求平均值、最大值和最大值下标、并查找里面是否有 8 如果有返回下标

public class ArrayExercise02 {
	public static void main (String[] args) {


		//2. 随机生成10个1-100整数保存到数组,并降序打印及求平均值、最大值
		//	和最大值下标、并查找里面是否有 8

		/*
		思路分析:
			1. 循环生成10个随机数,并保存到数组
			2. 对数据进行排序
			3. 打印数组, 并计算平均值, 打印最大值及其下标, 并查找有无 8		
		*/

		// 创建数组,储存随机数
		int randomArray[] = new int[10];
		// 循环数组
		for (int index = 0; index < randomArray.length; index++) {
			// 生产随机数并填充到数组中
			randomArray[index] = (int)(Math.random() * 100) + 1;

		}
		// 排序临时交换空间
		int swapSpace;
		// 排序(冒泡排序)
		for (int maxIndex = 0; maxIndex < randomArray.length - 1; maxIndex++) {
			for (int comparedIndex = maxIndex + 1; comparedIndex < randomArray.length; comparedIndex++) {
				if (randomArray[maxIndex] < randomArray[comparedIndex]) {
					swapSpace = randomArray[maxIndex];
					randomArray[maxIndex] = randomArray[comparedIndex];
					randomArray[comparedIndex] = swapSpace;

				}
			}
		}
		// 寻找结果
		byte findResult = -1;
		// 求和
		int sum = 0;
		// 遍历排序后的数组
		for (int index = 0; index < randomArray.length; index++) {
			// 累加			
			sum += randomArray[index];
			// 打印
			System.out.print(randomArray[index] + " ");
			// 寻找有无8
			if (randomArray[index] == 8) {
				findResult = (byte)index;
			}
		}
		System.out.println();
		// 打印平均值
		System.out.println("平均值: " + (sum / ((double)randomArray.length)));
		// 打印最大值
		System.out.println("最大值: " + randomArray[0] + "\n索引为: 0");
		// 寻找 8
		switch (findResult) {
			case -1:
				System.out.println("8 不存在" );
				break;
			default:
				System.out.println("有8 索引为: " + findResult);
		}

	}
}
posted @   假文艺青年。  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示