为了账号安全,请及时绑定邮箱和手机立即绑定

数据结构之数组重写

数据结构研究的是什么?

数据结构研究的是数据如何在计算机中进行组织和存储,使我们可以高效的获取数据和修改数据。

数据结构的结构类型

线性结构

  • 数组

  • 队列

  • 链表

  • 哈希表

树结构

  • 二叉树

  • 二分搜索树

  • AVL

  • 红黑树

  • Treap

  • Splay

  • Trie

图结构

  • 邻间矩阵
  • 邻接表

学习目的

在之后工作中我们需要的应用场景的不同,灵活的选择最合适的数据结构,来解决相应的问题。

数据结构典型例子

  • 数据库
  • 操作系统
  • 文件压缩

数组重写

最终效果

定义数组的容量,实现对数组添加元素。

array类的代码实现。

public class array {
    private int[] data;  //数组
    private int size;    //个数   索引+1
    /**
     * 有参数的构造函数,传入数组的容量capacity构造array
     * @param capacity
     */
    public array(int capacity) {
        data = new int[capacity];
        size = 0;
    }
    /**
     * 无参数的构造函数,默认数组的容量capacity=10
     */
    public array(){
        this(10);
    }
    /**
     * 数组元素的个数
     * @return
     */
    public int getsize(){
        return size;
    }
    /**
     * 数组的容量
     * @return
     */
    public int getCapacity(){
        return data.length;
    }
    /**
     * 数组是否为空
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 向数组的所有元素后添加一个元素
     * @param e
     */
    public void addLast(int e){
        add(size,e);
    }
    public void addFirst(int e){
        add(0,e);

    }
    /**
     * 向某一个索引的位置插入一个元素,之后所有的元素都向后移动一个位置  注意:是从后向前挪动,因为从前向后挪动会覆盖后一位索引的元素
     * @param index
     * @param e
     */
    public void add(int index,int e){
        if(size == data.length){
            throw new IllegalArgumentException("add is already full");
        }
        if(index < 0 || index>size){
            throw new IllegalArgumentException("add failed request index >=0");
        }
        for(int i = size-1;i>=index;i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size ++;
    }

    /**
     * 获取index索引位置的元素
     * @param index
     * @return
     */
    int get(int index){
        if(index< 0 || index>=size){
            throw new IllegalArgumentException("get failed , index is illegal");
        }
        return data[index];
    }
    void set(int index,int e){
        if(index< 0 || index>=size){
            throw new IllegalArgumentException("get failed , index is illegal");
        }
        data[index] = e;
    }


    /**
     * 重写输出
     * @return
     */
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d\n",size,data.length));
        res.append('[');
        for(int i=0;i<size;i++){
            res.append(data[i]);
            if(i != size-1) {
                res.append(", ");
            }
        }
        res.append(']');
        return res.toString();
    }
}

主方法实现。

public class Main {
    public static void main(String[] args) {
        array arr = new array(20);
        for(int i=0;i<10;i++){
            arr.addLast(i);
        }
        System.out.println(arr);
        arr.add(1,100);
        System.out.println(arr);
        arr.addFirst(-1);
        System.out.println(arr);
    }
}

实现效果图。

图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消