数组常见操作
-
遍历
/** * @ClassName: ArrayDemo4 * @Description: TODO * @Author: benjamin * @Date: 2019/3/23 11:27 * @Version: 1.0 */public class ArrayDemo4 { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7}; System.out.println("length:"+arr.length); //打印数组的长度 for(int i = 0;i=0;i--){ //数组倒序的遍历输出 System.out.println("arr["+i+"] = "+arr[i]+";");//arr[0]=1,....; } System.out.println(" "); for(int i =0;i
-
最值
两个思路:一是比较最大的值;二是找到最大的下标,然后返回最大的值。
/** * @ClassName: ArrayDemo5 * @Description: TODO * @Author: benjamin * @Date: 2019/3/23 11:39 * @Version: 1.0 */public class ArrayDemo5 { public static void main(String[] args) {// int[] array = new int[]{-1,-9,-8,-2}; int[] array = new int[]{22,3,1,343,455,24,5}; System.out.println("max = "+getMaxNum(array)); System.out.println("max = "+getMaxNum_2(array)); } /* 获取数组中的最大值; * 思路: * 1. 需要进行比较,并定义变量记录每次比较后较大的值; * 2. 对数组中的元素进行遍历取出,和变量中记录的元素进行比较; * 如果遍历到的元素大于变量中记录的元素,就用变量记录住该大的值; * 3. 遍历结果,该变量记录就是最大的值; * */ public static int getMaxNum(int[] array){ // int maxValue = array[0]; for(int i = 1; i=maxValue) maxValue = array[i]; } return maxValue; } public static int getMaxNum_2(int[] array){ //找到最大的下标,返回最大的值array[maxIndex] int maxIndex = 0; for(int i = 1; i =array[maxIndex]) maxIndex = i; } return array[maxIndex]; }}
-
排序
-
选择排序
/** * @ClassName: SelectSort * @Description: 选择排序 * @Author: benjamin * @Date: 2019/3/24 15:23 * @Version: 1.0 */public class SelectSort { public static void main(String[] args) { //定义一个数组 元素类型[] 数组名 = new 元素类型 [] {元素1,元素2...} int [] array = new int[]{54,26,93,17,77,313,44,55,20}; selectSort(array);//选择排序 //打印数组; for(int i =0;i
array[j]){// int temp = array[i];// array[i] = array[j];// array[j] = temp; swap(array,i,j); } } } } //交换函数; public static void swap(int[] array,int a,int b){ int temp = array[a]; array[a] = array[b]; array[b] = temp; }} -
冒泡排序
/** * @ClassName: BubbleSort * @Description: 冒泡排序 * @Author: benjamin * @Date: 2019/3/24 17:06 * @Version: 1.0 */public class BubbleSort { public static void main(String[] args) { int[] array = new int[]{54,26,93,17,77,313,44,55,20}; bubbleSort(array); //打印数组; for(int i =0;i
array[j+1]){// int temp = array[j];// array[j] = array[j+1];// array[j+1] = temp; swap(array,j,j+1); } } } } //交换函数; public static void swap(int[] array,int a,int b){ int temp = array[a]; array[a] = array[b]; array[b] = temp; }} -
-
折半查找(二分查找)
/** * @ClassName: ArrayDemo7 * @Description: 数组常见功能查找 * @Author: benjamin * @Date: 2019/3/24 20:55 * @Version: 1.0 */public class ArrayDemo7 { public static void main(String[] args) { int[] array = new int[]{12,23,34,55,56}; int key = getIndex(array,23); System.out.println("key:"+key); int value = binarySearch(array,55); int value2 = binarySearch_2(array,56); System.out.println("value:"+value); System.out.println("value2:"+value2); } //数组常见的功能:通过key查找数组中对应的元素 public static int getIndex(int[] array,int key){ for(int i =0;i
array[mid]){ //如果所给值大于中间数,则保留mid右边 low = mid + 1; }else if(value < array[mid]){ //如果所给值小于中间数,则保留mid左边 high = mid - 1; } if (high < low){ // 代表循环结束 return -1; } mid = (high+low)/2; } return mid; } public static int binarySearch_2(int[] array,int value){ int low = 0; int high = array.length-1; while(low <= high){ //只要low int mid = (low + high) / 2; if(value > array[mid]){ low = mid + 1; }else if(value < array[mid]){ high = mid - 1; }else{ return mid; } } return -1; }}
面试题:给定一个有序的数组,若果往该数组中存储一个元素,并保证这个数组还是有序的,那么这个元素的存储的角标如何获取?
数组,有序,优先想到二分查找
/** * @ClassName: ArrayDemo8 * @Description: * 给定一个有序的数组, * 如果往该数组中存储一个元素,并保证这个数组还是有序的, * 那么这个元素的存储的角标如何获取? * @Author: benjamin * @Date: 2019/3/24 21:45 * @Version: 1.0 */public class ArrayDemo8 { public static void main(String[] args) { int[] array = new int[]{12,23,34,55,56}; int index = binarySearch(array,40); System.out.println("该放在"+index+"的位置"); } public static int binarySearch(int[] array,int value){ int low = 0; int high = array.length-1; while(low <= high){ int mid = (low + high) / 2; if(value > array[mid]){ low = mid + 1; }else if(value < array[mid]){ high = mid - 1; }else{ return mid; } } return low;//此时low是大于high的值,也就是放入的这个数在low的位置; }}