标签:setname block 抛出异常 大数 pre ati gen throws java程序
对于线性表的顺序存储结构来说,我们要实现getElement操作,即将线性表的第i个位置元素返回即可
插入算法思路:
1、如果插入位置不合理,抛出异常
2、如果插入表的长度大于等于数组长度,则抛出异常或动态增容
3、从最后一位元素开始向前遍历到第i个位置,分别把它们都向后移动一个位置
4、将要插入元素填入位置i处
5、表长度加1
1、如果删除位置不合理,抛出异常
2、取出插入元素
3、从删除元素位置开始遍历到最后一个元素位置,分别将它们都向前移动一位
4、表长度减一
public interface List { public Object getElement(int index) throws Exception; public void doInsert(Object obj,int index) throws Exception; public void doDelete(int index) throws Exception; }
public class SequenceList implements List{ static final int DEFUALTLENTH=10; int maxSize;//最大数组长度 int size;//当前长度 Object[] list;//对象数组 //顺序表初始化方法 public void init(int size1){ maxSize = size1;//这个表的长度为size1 list = new Object[size1]; } //无参构造方法 public SequenceList() { init(DEFUALTLENTH); } //有参构造方法 public SequenceList(int size2){ init(size2); } @Override public Object getElement(int index) throws Exception { if(size==0 || index<0 || index>=size){ System.out.println("参数不正确"); } if(size == maxSize){ System.out.println(""); } return list[index]; } @Override public void doInsert(Object obj,int index) throws Exception { if(index<0 || index>size+1){ System.out.println("参数错误"); } if(size == maxSize){ System.out.println("线性表已满,无法插入"); } for(int i=size-1;i>index-1;i--){ list[i+1]=list[i];//指定位置之后的元素都后移一位 } list[index] = obj; size++; } @Override public void doDelete(int index) throws Exception { if(index<0||index>=size){ System.out.println("参数错误"); } if(index<size){ for(int i=size-1;i>index;i--){ list[i-1] = list[i]; } } size--; } }
public class ListTest { public static void main(String args[]){ SequenceList seq = new SequenceList(20); try { seq.doInsert("aa", 1); seq.doInsert("bb", 2); seq.doInsert("cc", 3); for(int j=0;j<seq.size;j++){ System.out.println(seq.getElement(j));} } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class StudentList { public static void main(String args[]){ SequenceList seqList = new SequenceList(30); try { seqList.doInsert(new Student(1,"小米","女",23), seqList.size);//每次插入,都在表的最后一位插入 seqList.doInsert(new Student(2, "小徐", "男", 22), seqList.size); for(int i=0;i<seqList.size;i++){ System.out.println("第"+(i+1)+"学生信息为:"+seqList.getElement(i).toString()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Student{ private int id; private String name; private String gender; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]"; } public Student(int sid,String name,String gender,int age){ this.id = sid; this.name = name; this.gender = gender; this.age = age; } }
大话数据结构(五)(java程序)——顺序存储结构的插入与删除
标签:setname block 抛出异常 大数 pre ati gen throws java程序
原文地址:http://www.cnblogs.com/snowwang/p/6038073.html