數組是應用最普遍的數據存儲結構。它被植入到大部分編程語言中。因爲數組十分易懂,因此它被用來做爲介紹數據結構的起步點,並展現面向對象編程和數據結構之間的相互關係。java
Java中兩種數據類型:基本類型和對象類型。在許多編程語言中(如C++),數組也是基本類型,但在Java中把它們看成對象來對待,所以在建立數組時必須使用new操做符:編程
int[] intArray;
intArray = new int[100];
複製代碼
[] 操做符對編譯器來講是一個標誌,它說明正在命名的是數組對象而不是普通的變量。還能夠經過另外一種語法來使用這個操做符,將它放在變量名的後面,而不是類型後面:數組
int intArray[] = new int[100];
複製代碼
可是將[] 放在int後面會清楚地說明[]是數據類型的一部分,而不是變量名的一部分。微信
數組有一個length字段,經過它能夠得知當前數組大小(數據項的個數):數據結構
int arrayLength = intArray.length;
複製代碼
正如大多數編程語言同樣,一旦建立數組,數組大小便不可改變。編程語言
數組數據項經過使用方括號中的下標數來訪問。這與其餘語言相似:spa
temp = intArray[3];
intArray[7] = 66;
複製代碼
若是訪問小於0或比數組大小大的數據項,程序會出現Array Index Out of Bounds(數組下標越界)的運行時差錯誤。數組的下標是從0開始的,也就是說第一個數據項的下標是0。code
當建立數組以後,若是不另行指定,基本類型數組會自動賦值爲0或0.0而對象數組會自動賦值爲null對象。使用下面的語法能夠對一個基本類型的數組初始化,賦入非空值:cdn
int[] intArray = {0,3,6,9,12,15};
複製代碼
/** * HighArray對數組基本操做進行封裝 */
public class HighArray {
private long [] a;
private int nElems;
public HighArray(int max){
a = new long[max];
nElems = 0;
}
/** * 查找 */
public boolean find(long searchKey){
int j;
for(j = 0; j < nElems;j++){
if(a[j] == searchKey){
break;
}
}
if(j == nElems){
return false;
}
else{
return true;
}
}
/** * 插入 * @param value */
public void insert(long value){
a[nElems] = value;
nElems++;
}
/** * 刪除 * @param value * @return */
public boolean delete(long value){
int j;
for(j = 0; j < nElems;j++){
if(value == a[j]){
break;
}
}
if(j == nElems){
return false;
}else{
// 移動後面的元素
for (int k = j; k < nElems;k++){
a[k] = a[k+1];
}
nElems--;
return true;
}
}
/** * 遍歷數組元素 */
public void display(){
for(int j = 0; j < nElems; j++){
System.out.print(a[j]+" ");
}
System.out.println();
}
}
複製代碼
假設一個數組,其中的數據項按關鍵字升序排列,即最小值在下標爲0的單元上,每個單元比前一個單元的值大。這種數組被稱爲有序數組。將數據按順序排序的好處就是能夠經過二分查找顯著地提升查找速度。對象
當向這種數組中插入數據項時,須要爲插入操做找到正確的位置:恰好在稍小值的後面,稍大值的前面。而後將全部比待插入的數據項大的值向後移以便騰出空間。
二分查找使用的方法與咱們在孩童時期常玩的猜數遊戲中所用的方法同樣。在這個遊戲裏,一個朋友會讓你猜她正想的一個1至100之間的數。當你猜來一個數後,它會告訴你三種選擇中的一個:你猜的比她想的大,或小,或猜中了。
爲了能用最少的次數猜中,必須從50開始猜。若是她說你猜得過小,則推出那個數在51至100之間,因此下一次猜75。但若是她說有些大,則推出那個數在1至49之間,因此下一次猜25。
每猜一次就會將可能的值劃分紅兩部分。最後範圍會縮小到一個數字那麼大,那就是答案。
public class OrdArray {
private long[] a;
private int nElems;
public OrdArray(int max){
a = new long[max];
nElems = 0;
}
public int size(){
return nElems;
}
// 二分查找
public int find(long searchKey){
int lowerBound = 0;
int upperBound = nElems - 1;
int curIn;
while (true){
curIn = (lowerBound + upperBound) / 2;
if(a[curIn] == searchKey){
return curIn;
}else if (lowerBound > upperBound){
return nElems; // can't find it
}else {
if(a[curIn] < searchKey){
lowerBound = curIn + 1;
}else {
upperBound = curIn - 1;
}
}
}
}
public void insert(long value){
int j;
// 找到待插入的位置
for(j = 0; j < nElems;j++){
if(a[j] > value){
break;
}
}
// 騰出空間
for(int k = nElems;k > j;k--){
a[k] = a[k-1];
}
a[j] = value;
nElems++;
}
public boolean delete(long value){
int j = find(value);
if(j == nElems){
return false;
}else{
for (int k = j;k < nElems;k++){
a[k] = a[k+1];
}
nElems--;
return true;
}
}
public void display(){
for(int j = 0; j < nElems; j++){
System.out.print(a[j]+" ");
}
System.out.println();
}
}
複製代碼
歡迎留言補充,共同交流。我的微信公衆號求關注: