java 數(shù)組對象

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;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,906評論 0 33
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,652評論 18 399
  • 回溯算法 回溯法:也稱為試探法,它并不考慮問題規(guī)模的大小,而是從問題的最明顯的最小規(guī)模開始逐步求解出可能的答案,并...
    fredal閱讀 13,993評論 0 89
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 2,075評論 0 2
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,496評論 0 6

友情鏈接更多精彩內(nèi)容