package com.pb.datestruture.base; /** * 线性表按顺序存储 add * Object为传入的内容 int为传入的索引 * @author Administrator * */ public class SequenceList implements LinearList { private Object [] slist; private int size; public SequenceList() { this(3); } public SequenceList(int length) { if(length==0){ this.slist=new Object[3]; } this.slist=new Object[length]; } //返回线性表的长度 public boolean isEmpty() { if(size==0){ return true; }else{ return false; } } //返回线性表容量大小 public int size() { return size; } public Object get(int index) { return (Object)slist[index]; } //修改index要修改的的索引 element要修改的内容 public void set(int index, Object element) { cheIndex(index); slist [index] = element; } public boolean add(Object element) { return add(size,element); } //验证索引是不是符合要求 private void cheIndex(int index){ if(index>size||index<0){ throw new IndexOutOfBoundsException("index"+index+",size"+size); } } //添加index要修改的的索引 element要体添加的内容 public boolean add(int index, Object element) { cheIndex(index); if(index>size||index<0){ throw new IndexOutOfBoundsException("index"+index+",size"+size); } if(size==slist.length){ Object[] temp=slist; this.slist=new Object[temp.length*2]; for(int j=0;j<temp.length;j++){ this.slist[j]=temp[j]; } } for(int i=size-1;i>=index;i--){ slist[i+1]=slist[i]; } slist [index] =element; size++; return true; } ////删除index要删除的的索引 public Object remove(int index) { cheIndex(index); Object old = (Object)slist[index]; //依次向后挪动 for(int i=index;i<size-1;i++){ slist[i]=slist[i+1]; } //释放最后1个元素 slist[--size]=null; return old; } //清除线性表 public void clear() { if(size!=0){ for(int i=0;i<size;i++){ slist[i]=null; } size=0; } } /** * @param args */ public static void main(String[] args) { // SequenceList s = new SequenceList(); // System.out.println("Is Empty?"+s.isEmpty()); // System.out.println("Size:"+s.size); // s.add("Jack"); // s.add("John"); // s.add(0,"Lily"); // s.add("Lucy"); // s.set(2, "LiLei"); // s.remove(2); // s.clear(); // System.out.println("Is Empty?"+s.isEmpty()); // System.out.println("Size:"+s.size); // System.out.println(s.get(0)); // System.out.println(s.get(1)); // System.out.println(s.get(2)); } }

微信网友_5866787043627008 LV1
2022年3月10日
人生赢家° LV1
2021年11月28日
zfx88666 LV1
2021年10月27日
江左右 LV1
2021年10月12日
zhanglizhe0128 LV1
2021年6月10日
紫气东来 LV1
2021年5月28日
肖俊 LV1
2021年5月13日
独角兽的眼泪 LV1
2021年2月26日
李海洋 LV12
2020年11月8日
yufan宇梵 LV2
2020年9月12日

crazy11crazy LV30
1月23日
zhouguoqiang LV1
2024年12月11日
1220261962d
2024年10月15日
暂无贡献等级
sunlea LV20
2024年5月8日
推墙大师 LV1
2024年1月2日
bssheep LV1
2024年1月1日
高李杰 LV2
2023年12月26日
1112WHQ LV7
2023年11月3日
Seaskye LV14
2023年11月2日
shuangfu LV25
2023年9月12日