⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sequencelist.java

📁 以数组为存储结构保存线性表
💻 JAVA
字号:
//2.  接口实现及测试
//SequenceList.java 


public class SequenceList implements List{
  private int max=-1;
  private int current=-1;
  private Object[] objectArray;
  public SequenceList(int a){
    max = a;
    initList();
   }
  public void initList(){
    objectArray = new Object[max];
    current = 0;
   } 

 public void clear(){
  objectArray = new Object[max];
    current = -1;
 }
 public int getLength(){
  return current+1;
 }
 public Object getEntry(int i){
  if(i >current || i <0){
      return null;
     }
  return objectArray[i];
 }
 public int contains(Object anEntry){
  int flag=0;
  for(int i = 0;i<current;i++){
      if(anEntry.equals(objectArray[i])){
       flag=0; 
       return i+1;
      }
      else
       flag=1;
     }
  if(flag==0)
    System.out.println("元素"+anEntry+"存在!");
  else
   System.out.println("元素"+anEntry+"不存在!");
     return -1;
 }
 public boolean isFull(){
  if(current==max){
   System.out.println("链表已满!");
      return true;
     }
  else
   {System.out.println("链表未满!");
     return false;}
 }
 public boolean add(int i, Object newEntry){
    if(i > current || i <0){
     return false;
    }
    int j = current+1;
    while(j>i){
     objectArray[j] =objectArray[j-1];
     j--;
    }
    objectArray[i] = newEntry;
    current++;
    return true;
 }
 public Object remove(int i){
    if(i > current || i <0){
     return false;
    }
    while(i<current){
     objectArray[i] =objectArray[i+1];
     i++;
    }
    current--;
    return true;
 }
 public boolean add(Object newEntry){
  current=current+1;
  objectArray[current] =newEntry;
  return true;
 }
 public boolean replace(int givenPosition, Object newEntry){
  if(givenPosition>current||givenPosition<0){
   return false;
  }
  for(int i=0;i<current;i++){
   if(i==givenPosition)
    objectArray[i]= newEntry;
  }
  return true;
 }
 public static void main(String[] args){
  String aa="aa";
  String bb="bb";
  String cc="cc";
  String dd="dd";
  String ee="ee";
  String ff="ff";
  String gg="gg";
  String hh="hh";
  String ii="ii";
  SequenceList L=new SequenceList(10);  
  L.add(aa);
  L.add(bb);
  L.add(dd);
  L.add(ee);
  L.add(ff);
  L.add(gg);
  L.add(hh);
  System.out.println("原表为:");
  for(int i=1;i<L.getLength();i++)
      System.out.println(L.getEntry(i));
  L.add(3, cc);
  System.out.println("插入元素后的表为:");
  for(int i=1;i<L.getLength();i++)
      System.out.println(L.getEntry(i));
  System.out.println("表长="+L.getLength());
  System.out.println("第二个元素的值为="+L.getEntry(2));
  L.contains(ii);
  L.isFull();
  L.remove(6);
  System.out.println("删除元素后的表为:");
  for(int i=1;i<L.getLength();i++)
      System.out.println(L.getEntry(i));    
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -