数组 |
笔记Notes
|
int[] age = { 1, 2, 3, 4, 5, 6 };
/* * 数组中常见的算法 * 1. 求数组中元素的最大值、最小值、平均值、总和等 * 2. 数组的复制、反转、查找(线性查找、二分查找(了解)) * 3. 数组元素的排序算法:冒泡排序 */ public class ArrayAlgorithm { public static void main(String[] args) { // 数组最大值、最小值、平均值、总和 System.out.println("---------------------------------数组最大值、最小值、平均值、总和--------------------------"); int scores[] = new int[5]; for (int i = 0; i < scores.length; i++) { scores[i] = (int) (Math.random() * 10); } for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + " "); } System.out.println(); int max = scores[0]; int min = scores[0]; int add = scores[0]; double ever = 0; for (int i = 0; i < scores.length; i++) { if (max < scores[i]) { max = scores[i]; } if (min > scores[i]) { min = scores[i]; } add += scores[i]; } ever = add / scores.length; System.out.println("max = " + max + " min = " + min + " add = " + add + " ever = " + ever); System.out.println("-------------------------------------------------------------"); System.out.println("----------------------------数组复制---------------------------------------------"); int copyScores[] = new int[scores.length]; for (int i = 0; i < copyScores.length; i++) { copyScores[i] = scores[i]; } for (int i = 0; i < copyScores.length; i++) { System.out.print(copyScores[i] + " "); } System.out.println(); System.out.println("---------------------------数组反转方法一---------------------------------------"); // 方法一 System.out.println("方法一"); int reverse[] = new int[scores.length]; for (int i = 0, j = scores.length - 1; i < scores.length && j >= 0; i++, j--) { reverse[i] = scores[j]; } for (int i = 0; i < reverse.length; i++) { System.out.print(reverse[i] + " "); } System.out.println(); System.out.println("--------------------------"); for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + " "); } System.out.println(); System.out.println("----------------------------数组反转方法二-----------------------------------"); for (int i = 0; i < scores.length / 2; i++) { int temp = scores[i]; scores[i] = scores[scores.length - i - 1]; scores[scores.length - i - 1] = temp; } for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + " "); } System.out.println(); System.out.println("-------------------------数组查找-线性查找-------------------------"); int[] numbers = { 1, 10, 9, 8, 20 }; // 线性查找 int findNumber = 18; // 要查找数值 int index = -1;//查找到的数值所对应的下标 for (int i = 0; i < numbers.length; i++) { if (findNumber ==numbers[i]) { index = i; break;//只查找一个 } } if (index != -1) { System.out.println("查找到数值!所对应的数组下标为:" + index); } else { System.out.println("查找不到值!"); } System.out.println("------------------------数组查找- 二分查找--------------------------------"); int[] number2 = { 12, 30, -5, 6, 9, 10, 5 }; //排序 Arrays.sort(number2); // 必须排序好 for (int i = 0; i < number2.length; i++) { System.out.print(number2[i] + " "); } System.out.println(); int findNumber2 = 12;//要查找的内容 int start = 0; int end = number2.length-1; int index2 = -1; //所在元素位置 while (start <= end) { int mid = (start + end) / 2; if (number2[mid] == findNumber2) { index2 = mid; break; } if(number2[mid] > findNumber2){ end = mid - 1; }else { start = mid + 1; } } if (index2 != -1) { System.out.println("你要的商品存在,在位置:" + index2); }else { System.out.println("客官您的包裹丟了"); } System.out.println("------------------------------冒泡排序(必须会手写)-------------------------------------"); int[] numbers3 = {10,2,89,39,16}; for (int i = 0; i < numbers3.length; i++) { System.out.print(numbers3[i] + " "); } System.out.println(); System.out.println("------------------------------排序前--------------------------------------------------------"); for (int i = 0; i < numbers3.length -1; i++) { //外排序次数为长度-1 为5-1 = 4 for (int j = 0; j < numbers3.length -1 - i; j++) { //内排序次数每次递减 4 3 2 1 if (numbers3[j] > numbers3[j+1]) { int temp = numbers3[j]; numbers3[j] = numbers3[j+1]; numbers3[j+1] = temp; } } } System.out.println("--------------------------排序后-------------------------------------------"); for (int i = 0; i < numbers3.length; i++) { System.out.print(numbers3[i] + " "); } System.out.println(); System.out.println("------------------------排序完成------------------------------------"); } } |
总结Summary |
|
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。