博客
关于我
实现顺序表(Java)
阅读量:519 次
发布时间:2019-03-08

本文共 6020 字,大约阅读时间需要 20 分钟。

创建空的成员变量并用构造函数初始化

class MyArrayList {       public int[] elem;    public int usedSize;    public MyArrayList() {           this.elem = new int[5];        this.usedSize = 0;   }

打印顺序表

//打印顺序表    public void display() {           for(int i = 0;i < this.usedSize;i ++) {               System.out.print(this.elem[i] + " ");       }        System.out.println();    }

在pos位置新增一个元素

public void add(int pos,int data) {           if(pos < 0 || pos > this.usedSize) {               System.out.println("不合法!");            return;        }        if (this.usedSize == this.elem.length) {           //扩容            this.elem =Arrays.copyOf(this.elem,2*this.elem.length);        }        for (int i = this.usedSize - 1; i >= pos ; i--) {               this.elem[i + 1] = this.elem[i];        }        this.elem[pos] = data;        this.usedSize ++;    }

判定是否包含某个元素

//判定是否包含某个元素    public boolean contains(int toFind) {           for( int i = 0;i < this.usedSize; i++) {               if (this.elem[i] == toFind)  {                   return true;            }        }        return false;    }

查找某个元素对应的位置

//查找某个元素对应的位置    public int search (int toFind) {           for( int i = 0;i < this.usedSize; i++) {               if (this.elem[i] == toFind)  {                   return i;            }        }        return -1;    }

获取pos位置的元素

//获取pos位置的元素    public int getPos(int pos) {           if(pos < 0 || pos >= this.usedSize) {               return -1;        }       return this.elem[pos] ;    }

给pos位置设置为value

//给pos位置设为value    public void setPos(int pos,int value) {           if(pos < 0 || pos >= this.usedSize) {               return;        }        this.elem[pos] = value;    }

删除某个元素

//删除第一次出现的关键字key    //找到是否有toremmve    //将后面的元素赋值给前面    public void remove (int toRemove) {           int j = search(toRemove);        if(j == -1) {               System.out.println("没有该数字");            return;        }        for (int i = j ;i < this.usedSize - 1;i++) {               this.elem[i] = this.elem[i+1];        }        this.usedSize--;    }

获取顺序表长度

//获取顺序表长度    public int size() {           return this.usedSize;    }

清空顺序表

//清空顺序表    public void clear() {           this.usedSize = 0;    }

调用方法

public class Test {       public static void main(String[] args) {           MyArrayList myArrayList = new MyArrayList();        //新增        myArrayList.add(0,1);        myArrayList.add(1,2);        myArrayList.add(2,3);        myArrayList.add(3,4);        myArrayList.add(4,5);        myArrayList.add(5,6);        myArrayList.display();        //判断是否包含某个元素        System.out.println(myArrayList.contains(3));        //查找某个元素对应的位置        System.out.println(myArrayList.search(3));        //获取pos的位置        System.out.println(myArrayList.getPos(3));        //给pos位置设置值        myArrayList.setPos(3,5);        myArrayList.display();        //删除某个元素        myArrayList.remove(3);        myArrayList.display();        //获取顺序表长度        System.out.println(myArrayList.size());        //清空顺序表        myArrayList.clear();        myArrayList.display();    }}

完整代码

class MyArrayList {       public int[] elem;    public int usedSize;    public MyArrayList() {           this.elem = new int[5];        this.usedSize = 0;   }   //打印顺序表    public void display() {           for(int i = 0;i < this.usedSize;i ++) {               System.out.print(this.elem[i] + " ");       }        System.out.println();    }    //在pos位置新增一个元素    public void add(int pos,int data) {           if(pos < 0 || pos > this.usedSize) {               System.out.println("不合法!");            return;        }        if (this.usedSize == this.elem.length) {               //扩容            this.elem =Arrays.copyOf(this.elem,2*this.elem.length);        }        for (int i = this.usedSize - 1; i >= pos ; i--) {               this.elem[i + 1] = this.elem[i];        }        this.elem[pos] = data;        this.usedSize ++;    }    //判定是否包含某个元素    public boolean contains(int toFind) {           for( int i = 0;i < this.usedSize; i++) {               if (this.elem[i] == toFind)  {                   return true;            }        }        return false;    }    //查找某个元素对应的位置    public int search (int toFind) {           for( int i = 0;i < this.usedSize; i++) {               if (this.elem[i] == toFind)  {                   return i;            }        }        return -1;    }    //获取pos位置的元素    public int getPos(int pos) {           if(pos < 0 || pos >= this.usedSize) {               return -1;        }       return this.elem[pos] ;    }    //给pos位置设为value    public void setPos(int pos,int value) {           if(pos < 0 || pos >= this.usedSize) {               return;        }        this.elem[pos] = value;    }    //删除第一次出现的关键字key    //找到是否有toremmve    //将后面的元素赋值给前面    public void remove (int toRemove) {           int j = search(toRemove);        if(j == -1) {               System.out.println("没有该数字");            return;        }        for (int i = j ;i < this.usedSize - 1;i++) {               this.elem[i] = this.elem[i+1];        }        this.usedSize--;    }    //获取顺序表长度    public int size() {           return this.usedSize;    }    //清空顺序表    public void clear() {           this.usedSize = 0;    }}public class Test {       public static void main(String[] args) {           MyArrayList myArrayList = new MyArrayList();        //新增        myArrayList.add(0,1);        myArrayList.add(1,2);        myArrayList.add(2,3);        myArrayList.add(3,4);        myArrayList.add(4,5);        myArrayList.add(5,6);        myArrayList.display();        //判断是否包含某个元素        System.out.println(myArrayList.contains(3));        //查找某个元素对应的位置        System.out.println(myArrayList.search(3));        //获取pos的位置        System.out.println(myArrayList.getPos(3));        //给pos位置设置值        myArrayList.setPos(3,5);        myArrayList.display();        //删除某个元素        myArrayList.remove(3);        myArrayList.display();        //获取顺序表长度        System.out.println(myArrayList.size());        //清空顺序表        myArrayList.clear();        myArrayList.display();    }}

转载地址:http://uoanz.baihongyu.com/

你可能感兴趣的文章
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>