顺序表的初步学习---Java

顺序表的初步学习

线性表:是具有n个相同特性的数据元素的有限序列。
常见的线性表:顺序表,链表,栈,队列,字符串。

顺序表:是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。
顺序表分为:
1.静态顺序表:使用定长数组储存。
2.动态顺序表:使用动态开辟的数组储存。

适用不同场景:
1 .静态顺序表适用于确定知道需要存多少数据的场景.
2 .静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用.相比之下动态顺序表更灵活, 根据需要动态的分配空间大小.

顺序表练习
1.在 pos 位置新增元素
2.判定是否包含某个元素
3.查找某个元素对应的位置
4.获取 pos 位置的元素
5.删除第一次出现的关键字key
6.获取顺序表长度
7.清空顺序表
8.打印顺序表

import java.util.Arrays;

public class MyArrayList {
    public int [] elem ;
    public int usedSize;
    public static final int  intCapacity = 10;    //初始容量
    public MyArrayList(){
        this.elem = new int[intCapacity];
        this.usedSize=0;
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    //判断是否满了
    private boolean isFull(){
        if(usedSize==this.elem.length){
            return true;
        }
        return  false ;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if (pos<0 || pos>usedSize){
            throw new RuntimeException("pos位置不合法");
        }
        if (this.isFull()){
            System.out.println("扩容!");
            this.elem = Arrays.copyOf(this.elem,2*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 isEmpty(){
        return this.usedSize==0;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        if (isEmpty()){
            System.out.println("顺序表为空");
            return false ;
        }
        for (int i = 0; i <this.usedSize ; i++) {
            if (this.elem[i]==toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int search(int toFind) {
        if (this.isEmpty()){
            throw new RuntimeException("为空");
        }
        if (this.contains(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-1){
            throw new RuntimeException("pos位置不合法");
        }else
            return this.elem[pos];
        // 不需要判断是否为空  ,前面和usedSize 判断过了
//        if (isEmpty()){
//            System.out.println("顺序表为空");
//            return false;
//        }
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if (pos<0 || pos>usedSize-1){
            throw new RuntimeException("pos位置不合法");
        }
        this.elem[pos]=value;
    }
    //删除第一次出现的关键字key
    //先判断是否为空,然后查找删除的关键字对应的下标
    public void remove(int toRemove) {
        if (this.isEmpty()){
            throw new RuntimeException("为空");
        }
        int index = search(toRemove); //search方法已经判断了是否为空
        if (index==-1){
            System.out.println("没有要删除的key");
            return;
        }
        for (int i = index; 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;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值