package day05;
public class Demo1 {
//數(shù)組:? 一種特殊的對象
public static void main( String[] args ){
? ?1. 數(shù)組對象的特殊聲明 與創(chuàng)建形式 (動態(tài)初始化, 先創(chuàng)建,再初始化)
? ? ? ? ? ?int[]? arr? =? new? int[10];
?? ? ? ? ?for( int i=0; i獲取? ? ? int value = arr[i]; *? ? ? ? int value = arr[i]; * ? ? ? ?
? ?2.? 對數(shù)組內(nèi)元素進行-->獲取? ? ? int value = arr[i];
? 3.? 對數(shù)組內(nèi)元素進行--->賦值/修改 arr[i] = value; *
? ? 數(shù)組中元素的內(nèi)存分析: *??
? ? ? ? (1) 當創(chuàng)建數(shù)組對象時, 僅僅創(chuàng)建了一個容器對象;
? ? ? ?(2) 初始化賦值:?
? ? ? ? ? ? ? 如果數(shù)組中元素是基礎類型,那么 元素會被初始化為 基礎類型0;
? ? ? ? ? ? ? 如果數(shù)組中元素是引用類型,那么 元素會被初始化為 null;?
? ? ? 也就是說: 數(shù)組中并不存儲對象,而是存儲對象的引用; 在數(shù)組創(chuàng)建之初以及初始化,
? ? ? ?數(shù)組中的所有引用都指向了null,而不存在對象; 另外,數(shù)組也是對象, 那么二維數(shù)組
? ? ? 也僅僅是 每個數(shù)組元素 指向了一個數(shù)組而已;?
? ?數(shù)組通過index訪問的問題:?
? ? ? 數(shù)組通過index, 對數(shù)組中的元素進行訪問, index從0開始, 0~length-1;?
? ? ? ?1) 如果arr 指向 null, 訪問時會拋出 空指針異常;
? ? ? ?2) 如果 要訪問的index 不在 0~length-1 之間,那么會拋出 數(shù)組越界異常;? ? ? ? ?
? ? ? 數(shù)組的特點:?
? ? ? ? ? ? ? ? ? 1. 數(shù)組只能存儲同一種 數(shù)據(jù)類型的數(shù)據(jù)
? ? ? ? ? ? ? ? ? 2. 數(shù)組是會存儲到數(shù)組中的元素分配一個index, index為 0~length-1?
? ? ? ? ? ? ? ? ?3. 數(shù)組一旦初始化,長度固定;?
? ? ? ? ? ? ? ? ? 4. 數(shù)組中的元素與元素之間的內(nèi)存是連續(xù)的;? ??
? ? ? ? ? ? ? ? 另外: 數(shù)組的其他 ----> 容器 該具有的功能
? ? ? ? ? ? ? ? ? 1. 增加?
? ? ? ? ? ? ? ? ? 2. 修改
? ? ? ? ? ? ? ? ? 3. 查找??
? ? ? ? ? ? ? ? ? ? ? ? 1) 查找指定index的元素?
? ? ? ? ? ? ? ? ? ? ? ? 2) 查找指定元素的 index?
? ? ? ? ? ? ? ? 4. 判斷?
? ? ? ? ? ? ? ? ? ? ? ? 1) 判空?
? ? ? ? ? ? ? ? ? ? ? ?2) 數(shù)組是否相等?
? ? ? ? ? ? ? 5. 刪除?
? ? ? ? ? ? ? ? ? ? ?1) 刪除指定index的元素?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?明顯,前面所有的操作都只涉及到了? 數(shù)組的查找,以及修改; ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?----> 數(shù)組是沒有增加功能的,在數(shù)組創(chuàng)建之初,就沒固定length, 且被初始化;?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?----> 數(shù)組是沒有刪除功能的,而是通過數(shù)組中元素的移動,覆蓋要刪除的元素,達到刪除的目的; ?
?/int[] arr = new int[10];for( int i=0; i必須要先有序
public static int halfSearch( int[] arr, int searchValue ){
int minIndex = 0;
int maxIndex = arr.length-1;
int midIndex;
while( minIndex <= maxIndex ){ //可以相等,那是最后一次比較
midIndex = (minIndex + maxIndex)/2;
if(? arr[midIndex]? <? searchValue? ){
minIndex = midIndex + 1;
}else if( arr[midIndex] > searchValue ){
maxIndex = midIndex - 1;
}
else
{
return midIndex;
}
}
return? -1;
}
//選擇排序
public static void selectSort( int[] arr ){
for( int i=0; i< arr.length-1 ; i++ ){ //如果只有一個元素,那么自然有序
for( int j=i+1; j< arr.length ; j++ ){ //n個元素只需要比較n-1次
if( arr[i] > arr[j] ){
int temp = arr[i];
arr[i]? = arr[j];
arr[j]? = temp;
}
}
}
}
//獲取最大數(shù),交換到index==0的位置
public static void exchangeMaxToFirst( int[] arr ) {
if( arr.length == 0 ){
return;
}
int indexOfMaxValue = 0;
for( int i=1; i< arr.length; i++ ){
if( arr[0] < arr[i]){
indexOfMaxValue = i;
}
}
if( indexOfMaxValue != 0 ){
int temp = arr[indexOfMaxValue];
arr[indexOfMaxValue] = arr[0];
arr[0]? = temp;
}
}
public static int getMax( int[] arr ) throws Exception {
int max;
max = arr[0];
for( int i=1; i< arr.length ; i++ ){
if(? max < arr[i]? ){
max = arr[i];
}
}
return max;
}
}