顺序表的操作

1. 线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…

2、顺序表:在类中使用的数组,以便能面向对象

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改
在这里插入图片描述顺序表一般可以分为:

  • 静态顺序表:使用定长数组存储。
  • 动态顺序表:使用动态开辟的数组存储。

1、接口的实现

(1)实现一个顺序表的类

public class MyArrayList {
    public int[] elem;
    public int usedSize;//表示数组的长度。
    //使用构造方法初始化数组。
    public MyArrayList(){
        this.elem=new int[10];
    }
 }

(2)打印数组

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

(3)判断数组满没满

 //判断满没满。
    public boolean isfull(){
        return this.usedSize==this.elem.length;
    }

(4)判断数组是否为空

public boolean isEmpty(){
        return this.usedSize==0;
    }

(5)在 pos 位置新增元素

    public void add(int pos, int data) {
        if(pos<0||pos>usedSize){
            System.out.println("pos位置不合法");
            return;
        }
        if (isfull()){
            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++;
    }

(6)判定是否包含某个元素

    public boolean contains(int toFind) {
        for (int i = 0; i <this.usedSize; i++) {
            if(this.elem[i]==toFind){
                return true;
            }
        }
        return false;
    }

(7)查找某个元素对应的位置

  
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i]==toFind){
                return i;
            }
        }
        return -1;//没有查找到返回-1
    }

(8)获取 pos 位置的元素

    public int getPos(int pos) {
        if(pos<0||pos>this.usedSize){
            System.out.println("位置不合法");
            return -1;
        }
        if (isEmpty()){
            System.out.println("顺序表为空");
            return -1;
        }
        return this.elem[pos];
    }

(9)给 pos 位置的元素设为 value

 public void setPos(int pos, int value) {
        if(pos<0||pos>this.usedSize){
            System.out.println("位置不合法");
        }
        if (isEmpty()){
            System.out.println("顺序表为空");
        }
        for (int i = 0; i <this.usedSize; i++) {
            if (i==pos){
                this.elem[i]=value;
            }
        }
    }

(10)删除第一次出现的关键字key

    public void remove(int toRemove) {
        if (isEmpty()){
            System.out.println("顺序表为空");
        }
        int index=search(toRemove);
        if (index==-1){
            System.out.println("没有你要删除的数字");
            return;
        }
        for (int i = index; i <this.usedSize-1 ; i++) {
            this.elem[i]=this.elem[i+1];
        }
        this.usedSize--;

    }

(11)清空顺序表

    public void clear() {
        if (isEmpty()){
            System.out.println("已经为空");
        }
        this.usedSize=0;
    }

注:以上的代码都是放在MyArrayList类里实现的方法。

2、实例化顺序表对象

public class Demo1 {
    public static void main(String[] args) {
        MyArrayList myArrayList=new MyArrayList();
        System.out.println("在 pos 位置新增元素");
        myArrayList.add(0,1);
        myArrayList.add(1,2);
        myArrayList.add(2,3);
        myArrayList.add(3,4);
        myArrayList.add(4,5);
        System.out.println("打印数组");
        myArrayList.display();
        System.out.println("判断是否包含某个数据");
        System.out.println(myArrayList.contains(5));
        System.out.println(myArrayList.contains(7));
        System.out.println("查找某个数据的位置");
        System.out.println(myArrayList.search(2));
        System.out.println(myArrayList.search(8));
        System.out.println("获取某个位置上的数据");
        System.out.println(myArrayList.getPos(4));
        System.out.println(myArrayList.getPos(9));
        System.out.println("给 pos 位置的元素设为 value");
        myArrayList.setPos(3,15);
        myArrayList.display();
        System.out.println("删除第一次出现的关键字key");
        myArrayList.remove(15);
        myArrayList.display();
        System.out.println("获取顺序表长度");
        System.out.println(myArrayList.size());
        System.out.println("清空顺序表");
        myArrayList.clear();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值