線性表(儲存結構數組)--Java 實現

來源:互聯網
上載者:User

標籤:long   else   功能   color   class   []   操作   線性表   實現   

 1 /*線性表的數組實現 2  *特點:插入刪除慢需要平均移動一半的資料,尋找較快 3  *注意:有重複和無重複的資料對應的操作會有些不同 4  *注意數組一旦建立其大小就固定了 5  *Java集合長度可變是由於建立新的數組將原來舊的資料複製過去--這些附加功能犧牲了效率 6  *主要實現以下幾個功能 7  *1.線性表尋找 8  *2.線性表刪除 9  *3.線性表的插入儲存一個元素(無序數組插入)10  * */11 public class MyArray {12     13     private long[] arr;14     private int items;//記錄數組元素個數--關鍵15     16     public MyArray(int max) {//max記錄數組長度17         this.arr = new long[max];18         items = 0;19     }20     21     public int size(){22         return items;23     }24     //按照值尋找25     public boolean find(long searchKey){26         int index = 0;27         for(int i = 0; i < items; i++){28             if(searchKey == arr[i]){29                 index = i;30                 break;31             }32         }33         if(index == items){34             return false;35         }36         else{37             return true;38         }39     }40     41     //儲存元素42     public void insert(long key){43         arr[items] = key;44         items++;45     }46     47     //刪除一個元素--按照值48     public boolean delete(long key){49         //遍曆數組--先找到元素50         int i;51         for(i = 0; i < items; i++){52             if(key == arr[i]){53                 break;54             }55         }56         if(i== items){57             return false;58         }59         else{60             //向左邊移動操作61             for(int k = i;k < items; k++){62                 arr[k] = arr[k+1];63             }64             items--;65             return true;66         }67     }68     69     public void displayArray(){70         for(int i = 0;i < items;i++){71             System.out.print(arr[i] + " ");72         }73         System.out.println();74     }75 }

 

線性表(儲存結構數組)--Java 實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.