用java數組實現基本鏈表和可自擴充的鏈表

來源:互聯網
上載者:User

package com.tongji.szx.base;

import com.tongji.szx.baseDao.ListInterface;

public class ExtendList<T> implements ListInterface<T>{
 
 private T[] entry;
 private int length;
 private int capacity;
 private static final int BASE_LENGTH=10;
 
 /**
  * 預設的建構函式
  */
 public ExtendList(){
  this(BASE_LENGTH);
 }
 @SuppressWarnings("unchecked")
 /**
  * 提供一個初始鏈表長度的建構函式
  */
 public ExtendList(int intLength){
  if(intLength>0){
   this.length=intLength;
   this.capacity=intLength;
   entry=(T[])new Object[intLength];
  }else{
   this.length=BASE_LENGTH;
   this.capacity=BASE_LENGTH;
   entry=(T[])new Object[BASE_LENGTH];
  }
 }
 @Override
 /**
  * 在鏈表的末尾插入元素
  */
 public boolean add(T anEntry) {
  // TODO Auto-generated method stub
  try{
   if(length==capacity){
    copyEntry();
   }
   entry[length]=anEntry;
   length++;
   return true;
  }catch(Exception e){
   return false;
  }
 }
 
 @SuppressWarnings("unchecked")
 private void copyEntry(){
  T[] newEntry=entry;
  this.capacity*=2;
  entry=(T[])new Object[this.capacity];
  for(int index=0;index<newEntry.length;++index){
   entry[index]=newEntry[index];
  }
 }
 @Override
 public boolean add(int index, T anEntry) {
  // TODO Auto-generated method stub
  try{
   while(index>=capacity){
    copyEntry();
   }
   if(index<length){
    moveEntry(index,anEntry);
    length++;
    return true;
   }else{
    entry[index]=anEntry;
    //length+=(index);
    int sub=index-length+1;
    length+=sub;
    return true;
   }
  }catch(Exception e){
   return false;
  }
  //return false;
 }
 
 /**
  * 當插入位置在length之前時移動元素
  * @param index
  * @param anEntry
  */
 private boolean moveEntry(int aPosition,T anEntry){
  if(aPosition<length){
   for(int index=length-1;index>=aPosition;++index){
    entry[index+1]=entry[index];
   }
   entry[aPosition]=anEntry;
   return true;
  }
  return false;
 }

 @Override
 public boolean contains(T anEntry) {
  // TODO Auto-generated method stub
  for(T t:entry){
   if(t.equals(anEntry)){
    return true;
   }
  }
  return false;
 }

 @Override
 public T get(int index) {
  // TODO Auto-generated method stub
  if(index<length){
   return entry[index];
  }
  return null;
 }

 @Override
 public boolean isEmpty() {
  // TODO Auto-generated method stub
  return (length==0);
 }

 @Override
 public boolean isFull() {
  // TODO Auto-generated method stub
  return (length==capacity);
  //return false;
 }

 @Override
 public T remove(int index) {
  // TODO Auto-generated method stub
  if(index<length){
   T newEntry=entry[index];
   for(int i=index;i<length;++i){
    entry[i]=entry[i+1];
   }
   length--;
   return newEntry;
  }
  return null;
 }

 @Override
 public T replace(int index, T anEntry) {
  // TODO Auto-generated method stub
  if(index<length){
   T newEntry=entry[index];
   entry[index]=anEntry;
   return newEntry;
  }
  return null;
 }
 @Override
 public int getLength(){
  return length;
 }
 public int getCapacity(){
  return this.capacity;
 }
 public void Display(){
  for(int index=0;index<length;++index){
   if(entry[index]!=null){
    System.out.println(index+":"+entry[index].toString());
   }else{
    System.out.println(index+":"+"對象為空白!");
   }
  }
 }
 
 public static void main(String[] args){
  System.out.println("====================");
  ExtendList<String> elst=new ExtendList<String>(5);
  elst.add("sunzhenxing");
  elst.add(10, "sunhailong");
  
  elst.Display();
  System.out.println(elst.getLength());
  System.out.println(elst.getCapacity());
  elst.remove(10);
  System.out.println(elst.getLength());
  elst.Display();
 }
 
 
 
 
 
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.