栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java顺序结构(用类实现顺序表)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java顺序结构(用类实现顺序表)

import java.util.Arrays;


public class MyArrayList {
    public int[] elem;
    //数据个数
    public int usedSize;

    public MyArrayList(){
        this.elem = new int[10];
    }

    // 打印顺序表
    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("pos 位置不和法");
            return;
        }

        //判断数组满不满,满的话扩容
        if (isFull()){
            this.elem = Arrays.copyOf(this.elem,2*usedSize);
        }

        //从后往前移
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        //在指定位置插入指定元素
        this.elem[pos] = data;
        this.usedSize++;
    }

    //判断满不满
    public boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }else {
            return false;
        }
    }
    // 判定是否包含某个元素
    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){
            System.out.println("pos位置不合法");
            return  -1;
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if (pos < 0 ||pos > this.usedSize){
            System.out.println("pos 位置不和法");
            return;
        }

        this.elem[pos] = value;
    }

    //判断顺序表是否为空
    public boolean isEmpty(){
        if (this.usedSize == 0){
            return true;
        }
        return false;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if (isEmpty()){
            System.out.println("顺序表为空");
            return;
        }
        for (int i = 0; i < this.usedSize; i++){
            if (this.elem[i] == toRemove) {
                for (;i < this.usedSize - 1; i++){
                    this.elem[i] = this.elem[i + 1];
                }
                usedSize --;
                return;
            }
        }
        System.out.println("要删除的值不存在");
    }
    // 获取顺序表长度
    public int size() {
        return usedSize;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773711.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号