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

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

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  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/

你可能感兴趣的文章
python中打散数组shuffle
查看>>
Python中执行外部命令
查看>>
Python中拼接numpy数组-ChatGPT4o作答
查看>>
Python中提示:no module named 'PIL'
查看>>
Python中提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
查看>>
python中数据类型存储_浅谈Python中的数据类型
查看>>
python中文件最重要的功能是什么-学 Python 都用来干嘛的?
查看>>
python中文版语言_高盛最新调查:Python超过汉语成为未来最重要技能,你准备学哪种编程语言?...
查看>>
Python中文问题SyntaxError
查看>>
Python中方括号和括号括起来的列表有什么区别?
查看>>
python中时间处理_Python中处理时间的几种方法小结
查看>>
Python中枚举串口(包括虚拟端口)的跨平台方法是什么?
查看>>
python中根号下a怎么写_python中根号怎么表示
查看>>
Python中正则表达式的巧妙使用
查看>>
Python中浮点数的二进制表示(位不是十六进制)
查看>>
Python中特殊函数和表达式 filter,map,reduce,lambda
查看>>
python中用socket检测端口_Python 用socket模块实现检测端口和检测web服务
查看>>
python中用于列表操作的plus和append有什么区别?
查看>>
python中用递归求和
查看>>
python中的@staticmethod
查看>>