实现一个动态顺序表-java

本文详细介绍了使用Java实现顺序表的各种基本操作,包括添加、删除、查找、设置元素等,并通过一个示例程序展示了如何使用自定义的顺序表类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要实现以下功能:
// 打印顺序表
public void display() { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int search(int toFind) { return -1; }
// 获取 pos 位置的元素
public int getPos(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { }

在这里插入图片描述
MyArrayList是放类和各种方法的。
TestDemo是放main函数,调用MyArrayList的程序的。

MyArrayList:

import java.util.Arrays;
public class MyArrayList {
    public int[] elem;//数组,此处想放什么类型都可以改,例如double,Person类型也可以,
                      // 只是在开头要class Person()一下
    public int usedSize;//有效的数据个数
    public static final int capacity = 10;//初始容量

    public MyArrayList() {
        this.elem = new int[capacity];
        this.usedSize = 0;
    }

    // 打印顺序表  
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
    // 在 pos 位置新增元素
    private boolean isFull(){
        if(this.usedSize == this.elem.length){
            return true;
        }
        return false;
    }
    private void checkPos(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new RuntimeException("pos位置不合法!");
        }
    }
    public void add(int pos, int data) {
        checkPos(pos);//检查pos位置是否合法,只要抛出异常,代码就停到这行不走了
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        /*if(pos < 0 || pos > usedSize){//检查是否合法的最原始的另一种方法
            return;
        }*/
        for(int i =usedSize - 1 ;i >= pos; i--){
            this.elem[i + 1] =this.elem[i];
        }
        this.elem[pos]=data;
        this.usedSize ++;//在TestDemo里面有一个添加数组元素的过程,由此usedSize++,才有了数值
    }
    // 判定是否包含某个元素   
     public boolean contains(int toFind) {
         for(int i = 0; i < this.usedSize; i++){
             if(toFind == this.elem[i]){
                 return true;
             }
         }
         return false;
     }
    // 查找某个元素对应的位置    
     public int search(int toFind) {
         for(int i = 0; i < this.usedSize; i++){
             if(toFind == this.elem[i]){
                 return i;
             }
         }
         return -1;
     }

     //获取 pos 位置的元素
    private boolean isEmpty(){
        return this.usedSize == 0;//不能写null,因为null是没分配内存,写0是分配内存,没有元素
    }
    public int getPos(int pos) {
        if(isEmpty()){ //1.顺序表是否为空
            //return -1;
            throw new RuntimeException("顺序表为空!");//手动抛出异常
        }
        /*if(pos < 0 || pos >= usedSize){ //2.合法性(负数,>usedSize)
            return -1;
        }*/
        checkPos(pos);//合法性
        return this.elem[pos];
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 给 pos 位置的元素设为 value    
     public void setPos(int pos, int value) {
        checkPos(pos);
        this.elem[pos]= value;
     }
    // 删除第一次出现的关键字key    
    public void remove(int toRemove) {
        /*int index = 0;
        for(int i = 0; i < this.usedSize - 1; i ++){
            if(toRemove == this.elem[i]){
                 index =i;
            }
        }*/
        int index = search(toRemove);
        if(index == -1){
            System.out.println("没有需要删除的数字!");
            return;
            //此处必要return,不然index=-1,还会继续往下执行!
        }
        for(int i = index; i <this.usedSize - 1; i++){
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }
    // 清空顺序表    
     public void clear() {
        this.usedSize = 0;//可用的有效数字为0
     }
}

TestDemo:

public class TestDemo {
    public static void main(String[] args) {
        MyArrayList myArrayList1 = new MyArrayList();
        for(int i = 0; i < 10; i ++){
            myArrayList1.add(i,i);
        }
        myArrayList1.display();
        myArrayList1.add(2,12);
        myArrayList1.add(10,20);
        //保证一个一个去放,才能扩容,不要跳着去放
        myArrayList1.display();
        System.out.println("=====================");
        System.out.println(myArrayList1.contains(51));
        myArrayList1.contains(5);
        System.out.println(myArrayList1.search(21));
        System.out.println("=====================");
        System.out.println(myArrayList1.size());
        System.out.println(myArrayList1.getPos(14));
        myArrayList1.remove(9);
        myArrayList1.clear();
        myArrayList1.display();
        myArrayList1.setPos(3,25);
        myArrayList1.display();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值