博客
关于我
顺序表各种功能的实现
阅读量:336 次
发布时间:2019-03-04

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

顺序表的功能实现

public class Demo09 {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add(0, 1);
myArrayList.add(1, 2);
myArrayList.add(1, 3);
myArrayList.add(2, 4);
myArrayList.show(); // 打印顺序表
myArrayList.add(3, 99);
myArrayList.show();
System.out.println(myArrayList.key(99)); // 判断是否有key元素,true为有
System.out.println(myArrayList.keyNum(99)); // 输出key元素的位置
System.out.println(myArrayList.getPos(3)); // 输出pos下标的元素
myArrayList.replace(3, 88); // 替换pos位置的元素为val
myArrayList.show();
myArrayList.del(88); // 删除第一次出现的关键字key
myArrayList.show();
System.out.println(myArrayList.getLegth()); // 获取长度
myArrayList.clear(); // 清空顺序表
myArrayList.show();
}
}
class MyArrayList {
public int[] elem;
public int usedSize;
public MyArrayList() {
this.elem = new int[1];
}
public boolean key(int key) {
for (int i = 0; i < this.usedSize; i++) {
if (elem[i] == key) {
return true;
}
}
return false;
}
public int keyNum(int key) {
for (int i = 0; i < this.usedSize; i++) {
if (elem[i] == key) {
return i;
}
}
return -1;
}
public int getPos(int pos) {
if (pos < 0 || pos >= this.usedSize) {
return -1;
}
return this.elem[pos];
}
public void replace(int pos, int val) {
if (pos < 0 || pos >= this.usedSize) {
System.out.println("pos不在范围内");
} else {
this.elem[pos] = val;
}
}
public void del(int key) {
for (int i = 0; i < this.usedSize; i++) {
if (elem[i] == key) {
for (; i < this.usedSize - 1; i++) {
elem[i] = elem[i + 1];
}
return;
}
}
}
public int getLegth() {
return this.usedSize;
}
public void clear() {
this.elem = new int[this.elem.length];
this.usedSize = 0;
}
public void add(int pos, int data) {
if (this.usedSize == this.elem.length) {
System.out.println("此表已满,需要扩容!");
expand();
}
if (pos < 0 || pos >= this.usedSize) {
return;
}
for (int i = this.usedSize - 1; i >= pos; i--) {
elem[i + 1] = elem[i];
}
elem[pos] = data;
this.usedSize++;
}
public void expand() {
int[] tmp = new int[this.elem.length * 2];
for (int i = 0; i < this.elem.length; i++) {
tmp[i] = this.elem[i];
}
this.elem = tmp;
}
public void show() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
}

以上代码实现了一个简单的顺序表类MyArrayList,主要功能包括:

  • 添加元素
  • 判断是否存在指定键值
  • 获取键值对应的位置
  • 获取指定位置的元素
  • 替换指定位置的元素
  • 删除第一次出现的指定键值
  • 获取顺序表长度
  • 清空顺序表
  • 打印顺序表内容
  • MyArrayList通过数组实现顺序表的功能,主要包括以下几个关键方法:

    • key(int key):检查顺序表中是否存在指定的键值
    • keyNum(int key):返回指定键值在顺序表中的位置
    • getPos(int pos):返回指定位置的元素
    • replace(int pos, int val):替换指定位置的元素
    • del(int key):删除第一次出现的指定键值
    • add(int pos, int data):添加元素(支持动态扩容)
    • expand():扩容方法
    • show():打印顺序表内容

    通过实际测试,可以发现该实现在基本功能上是完整的,但在性能上和容错性上还有提升空间。

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

    你可能感兴趣的文章
    Oracle 11g数据库成功安装创建详细步骤
    查看>>
    Oracle 11g超详细安装步骤
    查看>>
    Oracle 12c中的MGMTDB
    查看>>
    Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
    查看>>
    Oracle 9i数据库管理教程
    查看>>
    ORACLE Active dataguard 一个latch: row cache objects BUG
    查看>>
    oracle avg、count、max、min、sum、having、any、all、nvl的用法
    查看>>
    Oracle BEQ方式连接配置
    查看>>
    oracle Blob保存方式,oracle 存储过程操作blob
    查看>>
    Oracle BMW Racing sailing vessel帆船图
    查看>>
    ORACLE Bug 4431215 引发的血案—原因分析篇
    查看>>
    Oracle Business Intelligence Downloads
    查看>>
    Oracle cmd乱码
    查看>>
    Oracle Corp甲骨文公司推出Oracle NoSQL数据库2.0版
    查看>>
    【Docker知识】将环境变量传递到容器
    查看>>
    uniapp超全user-agent判断 包括微信开发工具 hbuilder mac windows 安卓ios端及本地识别
    查看>>
    Oracle DBA课程系列笔记(20)
    查看>>
    oracle dblink 创建使用 垮库转移数据
    查看>>
    oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
    查看>>
    Oracle dbms_job.submit参数错误导致问题(ora-12011 无法执行1作业)
    查看>>