Java实现顺序表

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
}

}

3.在pos位置插入元素

public void add(int pos,int data){

//检验 pos的合法性

if(pos < 0 || pos > this.usedSize){

return;

}

for (int i = this.usedSize-1; i >= pos ; i–) {

this.array[i+1] = this.array[i];

}

this.array[pos] = data;

this.usedSize++;

}

在这里插入图片描述

但上述代码有个问题,若数组内已经存满元素,便不能再直接插入元素,需要扩容

则改进后:

private boolean isFull(){

return this.usedSize==this.array.length;

}

//2.在pos位置插入data

public void add(int pos,int data){

//判断数组是否存满

if(isFull()){

//二倍扩容

this.array = Arrays.copyOf(this.array,2*this.array.length);

}

//检验pos的合法性

if(pos < 0 || pos > this.usedSize){

return;

}

for (int i = this.usedSize-1; i >= pos ; i–) {

this.array[i+1] = this.array[i];

}

this.array[pos] = data;

this.usedSize++;

}

注意:
  • 先要判断pos的合法性,负数、大于usedSize均不合法

  • 检验数组是否需要扩容

  • 挪数据,i 从usedSize-1 开始,[i+1]=[i]

  • 插入完成后,this.array[pos]=data

4.判断是否包含某个元素

public boolean contain(int toFind){

for (int i = 0; i < this.usedSize; i++) {

if(this.array[i] == toFind){

return true;

}

}

return false;

}

5.查找某个元素对应的位置

public int search(int toFind){

for (int i = 0; i < this.usedSize; i++) {

if(this.array[i] == toFind){

return i;

}

}

return -1;

}

6.获取pos位置的元素

public int getPos(int pos){

//①检验顺序表是否为空

if(isEmpty()){

throw new RuntimeException(“顺序表为空!”); //手动抛出异常

}

//②检验pos合法性

if(pos < 0 || pos >= usedSize){

throw new RuntimeException(“pos不合法!”);

}

return this.array[pos];

}

7.给pos位置的元素设为value

public void setPos(int pos,int value){

this.array[pos] = value;

}

8.删除第一次出现的关键字key

public void delete(int key){

int tmp = search(key);

if(tmp == -1){

System.out.println(“没有需要删除的数字!”);

return;

}

for (int i = tmp; i < this.usedSize-1; i++) {

this.array[i] = this.array[i+1];

}

this.usedSize–;

}

在这里插入图片描述

9.获取顺序表长度

public int len(){

return this.usedSize;

}

10.清空顺序表

public void clear(){

this.usedSize = 0;

}

以上是顺序表的所有操作

附全部代码:

顺序表:

import java.util.Arrays;

//1.新建一个顺序表

public class MyArrayList {

public int[] array; //数组

public int usedSize; //有效的数据个数

public static final int intCapacity = 10; //初始容量

public MyArrayList(){

this.array = new int[intCapacity];

this.usedSize = 0;

}

//2.打印顺序表

public void print(){

for (int i = 0; i < this.usedSize; i++) {

System.out.print(this.array[i]+" ");

}

System.out.println();

}

//判断数组是否存储满

private boolean isFull(){

return this.usedSize == this.array.length;

}

//3.在pos位置插入data

public void add(int pos,int data){

//判断数组是否存满

if(isFull()){

//二倍扩容

this.array = Arrays.copyOf(this.array,2*this.array.length);

}

//检验pos的合法性

checkPos(pos);

// if(pos < 0 || pos > this.usedSize){

// return;

// }

for (int i = this.usedSize-1; i >= pos ; i–) {

this.array[i+1] = this.array[i];

}

this.array[pos] = data;

this.usedSize++;

}

//4.判断是否包含某个元素

public boolean contain(int toFind){

for (int i = 0; i < this.usedSize; i++) {

if(this.array[i] == toFind){

return true;

}

}

return false;

}

//5.查找某个元素对应的位置

public int search(int toFind){

for (int i = 0; i < this.usedSize; i++) {

if(this.array[i] == toFind){

return i;

}

}

return -1;

}

//检验合法性 封装为方法

private void checkPos(int pos){

if(pos < 0 || pos > usedSize){

throw new RuntimeException(“pos不合法!”);

}

}

//判断顺序表是否为空

private boolean isEmpty(){

return this.usedSize == 0;

}

//6.获取pos位置的元素

public int getPos(int pos){

//①检验顺序表是否为空

if(isEmpty()){

//return -1;

throw new RuntimeException(“顺序表为空!”); //手动抛出异常

}

//②检验pos合法性

if(pos < 0 || pos >= usedSize){

最后

每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。

如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
表是否为空

if(isEmpty()){

//return -1;

throw new RuntimeException(“顺序表为空!”); //手动抛出异常

}

//②检验pos合法性

if(pos < 0 || pos >= usedSize){

最后

每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。

如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。

[外链图片转存中…(img-liNyb44k-1714453655011)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值