第二天:线性表之顺序储存
package cn.njupt.mdj; class linerTable<E>{ private Object[] data = null; private int capacity; private int current; //默认大小 public linerTable(){ this(10); } //初始化大小 开辟空间 public linerTable(int size){ if(size >= 0){ this.capacity = size; this.data = new Object[this.capacity]; this.current = 0; }else{ throw new RuntimeException("初始化大小需要大于0 :"+ size); } } //添加元素,由于是顺序表,元素依次向后填加 public boolean add(E object){ //注意这里不是 this.current == this.capacity-1,原因,添加元素后,this.current++了,使得比较的时候直接索引加了1位 if(this.current == this.capacity){ //扩充容量 this.capacity *= 2; //将原有的元素拷贝 Object[] newdata = new Object[this.capacity]; for(int i=0;i<this.current;i++){ newdata[i] = data[i]; } this.data = newdata; } //容量扩充后,或则本来空间就够,则向末尾添加元素 this.data[current] = object; this.current++; return true; } //有增加当然就有删除元素,删除,则肯定是删除指定位置的某个元素 public boolean delete(int index){ //首先删除之前,肯定是要判断index是否合理的 if(index < 0 || index > current){ throw new RuntimeException("无效的下标:" + index); //越界 } //下标既然合理,开始删除指定的元素,删除,即把该位置元素去掉 if(index == this.current-1){//末尾元素 this.data[current-1] = null; //直接删除 } //不是末尾元素,则所有元素向前补洞。 for(int i = index ;i<this.current-1;i++){ this.data[i] = this.data[i+1]; } --this.current; return true; } //有添加 删除 当然有插入 和查找 //插入元素,像指定位置插入 指定的元素 public boolean insert(int index,E object){ //插入元素,首先要保证插入的index是合理的数组范围内 if(index < 0 || index > current){ throw new RuntimeException("无效的下标:" + index); //越界 } //但同时要保证元素有空间可以给要插入的元素 if(this.current == this.capacity){ //扩充容量 this.capacity *= 2; //将原有的元素拷贝 Object[] newdata = new Object[this.capacity]; for(int i=0;i<this.current;i++){ newdata[i] = data[i]; } this.data = newdata; } //有了容量,开始插入,当然如果是向末尾插入,那就是添加元素了,但如果不是末尾,则就是整体元素后移了,所以获取当前元素位置 //System.out.println(current); for(int i= current; i > index ; i--){ this.data[i] = this.data[i-1]; } //空出的位置即是待插入的位置,若current<= index ,那么就是向末尾添加元素了 this.data[index] = object; this.current++; return true; } //查找元素,直接给定下标,返回要的元素 public E get(int index){ if(index < 0 || index > current){ throw new RuntimeException("无效的下标:" + index); //越界 } return (E)this.data[index]; } //获取当前元素的个数 public int size() { return current; } public void display(){ if(data == null || data.length == 0){ System.out.println("没有数据"); } for(int i=0;i < current;i++){ System.out.print(data[i].toString() + "、"); } } } public class ArrayList { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub linerTable l = new linerTable(20); //add l.add(1); l.add(2); l.add(3); l.add(5); l.add(6); l.add(7); l.add(9); l.display(); System.out.println(""); //insert l.insert(6,8); l.display(); System.out.println(""); //delete l.delete(6); l.display(); System.out.println(""); //get System.out.println(l.get(2)); } }
版本2:
package cn.njupt.mdj; class LineTable{ private int capacity; private Object[] data = null; private int current; public LineTable(){ this(3);//默认大小 } //初始化 public LineTable(int c){ capacity = c; data = new Object[capacity]; current = 0; } //增加,一次往后添加 public void add(Object ele){ //首先要判断元素是否已满,是否还有空间可以加入 if(isFull()){ //满了,可以通知,在这里采用扩容法进行 capacity *= 2; Object[] newData = new Object[capacity]; for(int i=0;i<current;i++){ newData[i] = data[i]; } this.data = newData; } //若没有满,则直接添加元素 data[current++] = ele; } public boolean isFull(){ return current == capacity; } //可以添加,当然可以删除, public Object remove(int index){ //判断元素是否已空 if(isEmpty()){ throw new RuntimeException("元素已经空了"); //越界 } //在判断index索引是否正确 if(index <0 || index >= current){ throw new RuntimeException("无效的下标:" + index); //越界 } //开始删除; Object temp = data[index]; for(int i=index;i<current;i++){ data[i] = data[i+1]; } //若 i>=current;如删除的末尾元素,则: data[current-1] = null; //指针后退 this.current--; return temp; } public boolean isEmpty(){ return current == 0; } //有增,删除,当然后有插入元素 public void insert(int index,Object ele){ //先判读空间是否还能插入 if(isFull()){ //满了,可以通知,在这里采用扩容法进行 capacity *= 2; Object[] newData = new Object[capacity]; for(int i=0;i<current;i++){ newData[i] = data[i]; } this.data = newData; }else{ //在判读,即使是插入也不能破坏顺序结构,依次往后插入 if(index < 0 || index > current){ throw new RuntimeException("无效的下标:" + index); //越界 } //开始插入, for(int i=current;i>index;i--){ data[i] = data[i-1]; } //若 index = current;在最后插入元素,和中间插入 this.data[index] = ele; current++; } } //有了插入,当然有查找某个元素 public Object find(int index){ if(index < 0 || index >= current){ throw new RuntimeException("无效的下标:" + index); //越界 } return data[index]; } //获取当前元素的个数 public int size() { return current; } } public class ArrayListDemo2 { public static void main(String args[]){ LineTable lineTable = new LineTable(4); lineTable.add(1); lineTable.add(2); lineTable.add(3); lineTable.add(4); lineTable.add(5); //删除 System.out.println(lineTable.remove(3)); //System.out.println(lineTable.remove(4)); //插入 lineTable.insert(3, 4); //再删除 System.out.println(lineTable.remove(4)); //查找 System.out.println(lineTable.find(2)); System.out.println("*****"); System.out.println("总个数为:"+lineTable.size()); //遍历 for(int i=0;i<lineTable.size();i++){ System.out.println(lineTable.find(i)); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端