數(shù)組 創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的黎川網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴! |
筆記Notes
|
int[] age = { 1, 2, 3, 4, 5, 6 };
/* * 數(shù)組中常見的算法 * 1. 求數(shù)組中元素的最大值、最小值、平均值、總和等 * 2. 數(shù)組的復(fù)制、反轉(zhuǎn)、查找(線性查找、二分查找(了解)) * 3. 數(shù)組元素的排序算法:冒泡排序 */ public class ArrayAlgorithm { public static void main(String[] args) { // 數(shù)組最大值、最小值、平均值、總和 System.out.println("---------------------------------數(shù)組最大值、最小值、平均值、總和--------------------------"); 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("----------------------------數(shù)組復(fù)制---------------------------------------------"); 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("---------------------------數(shù)組反轉(zhuǎn)方法一---------------------------------------"); // 方法一 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("----------------------------數(shù)組反轉(zhuǎn)方法二-----------------------------------"); 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("-------------------------數(shù)組查找-線性查找-------------------------"); int[] numbers = { 1, 10, 9, 8, 20 }; // 線性查找 int findNumber = 18; // 要查找數(shù)值 int index = -1;//查找到的數(shù)值所對(duì)應(yīng)的下標(biāo) for (int i = 0; i < numbers.length; i++) { if (findNumber ==numbers[i]) { index = i; break;//只查找一個(gè) } } if (index != -1) { System.out.println("查找到數(shù)值!所對(duì)應(yīng)的數(shù)組下標(biāo)為:" + index); } else { System.out.println("查找不到值!"); } System.out.println("------------------------數(shù)組查找- 二分查找--------------------------------"); 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;//要查找的內(nèi)容 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("------------------------------冒泡排序(必須會(huì)手寫)-------------------------------------"); 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++) { //外排序次數(shù)為長(zhǎng)度-1 為5-1 = 4 for (int j = 0; j < numbers3.length -1 - i; j++) { //內(nèi)排序次數(shù)每次遞減 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("------------------------排序完成------------------------------------"); } } |
總結(jié)Summary |
|