数据结构之数组代码实现

数据结构之数组代码实现

package com.algorithm.array;

/**
 * @author :Vimonster
 * @date: 2019-12-04 22:58:13
 * @slogan: 任时间再怎样低头呢喃,也要不挥泪后风雨兼程
 * @description:
 * 数据结构之数据组实现
 **/
public class ArrayImpl {

    private int size;		//数组的长度
    private int data[];
    private int index;		//当前已经存的数据大小

    /**
     * 数组初始化
     * @param size
     */
    public ArrayImpl(int size){
        this.size = size;
        data = new int[size];		//分配的内存空间{0,0,0,0,0}
        index = 0;
    }

    /**
     * 打印数组
     */
    public void print(){
        System.out.println("index:" + index);
        for(int i = 0 ; i < index ; i++){
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    /**
     * 给 loc 位置添加数据 n
     * @param loc
     * @param n
     */
    public void insert(int loc,int n){		//时间复杂度 O(n);
        if(index ++ < size){
            for(int i = size - 1; i > loc ; i --){	//为什么不能写size 0~size-1 如果loc是0 O(n),O(1)=>O(n)
                data[i] = data[i - 1];	//把数据往后移一个
            }
            data[loc] = n;
        }
        //扩容 会把size*2 0.75
    }

    /**
     * 删除数组中 loc 位置的数据
     * @param loc
     */
    public void delete(int loc){	//O(n)
        for(int i = loc ; i < size ; i++){
            if(i != size - 1){		//怕越界所以加一个判断
                data[i] = data[i + 1];
            }else{
                data[i] = 0;			//默认为0 就是没存数据的
            }
        }
        index -- ;
    }

    /**
     * 将数组中loc位置的数据更新为n
     * @param loc
     * @param n
     */
    public void update(int loc,int n){//O(1)
        if(loc > data.length - 1){
            System.out.println("更新的位置数组越界,请检查你更新的位置");
        }
        data[loc] = n;
    }

    /**
     * 获取数组中 loc 位置的值
     * @param loc
     * @return
     */
    public int get(int loc){		//O(1)
        if(loc > data.length - 1){
            System.out.println("获取的位置数组越界,请检查你获取的位置");
        }
        return data[loc];
    }


    public static void main(String[] args) {

        ArrayImpl array = new ArrayImpl(10);
        array.index = 10;
        array.data = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.println("原始数组为:");
        array.print();
        array.delete(0);
        System.out.println("删除0位置后的数组为: ");
        array.print();
        array.insert(0,0);
        System.out.println("给0位置添加数据0后数组为: ");
        array.print();
        array.update(2,10);
        System.out.println("将2位置更新数据为10后数组为: ");
        array.print();

    }

}


========测试结果=======

原始数组为:
index:10
0 1 2 3 4 5 6 7 8 9 
删除0位置后的数组为: 
index:9
1 2 3 4 5 6 7 8 9 
给0位置添加数据0后数组为: 
index:10
0 1 2 3 4 5 6 7 8 9 
给2位置更新数据为10后数组为: 
index:10
0 1 10 3 4 5 6 7 8 9 

Process finished with exit code 0

 

=========2.0版本=========

 

/**
 * @Author: Vimonster
 * @Date: Created in 2020/4/2 20:25
 * @motto: 任时间再怎样低头呢喃,也要不挥泪后风雨兼程、
 * @Description:2.0
 *  数组实现
 */
public class ArrayImpl {

    private int size;  //数组的长度
    private int[] data;
    private int index; //当前已经存在的数据大小

    /**
     * 数组初始化
     * @param size
     */
    private void init(int size) {

        this.index = 0;
        if(size >= 0) {
            this.size = size;
            this.data = new int[size];
        } else {
            System.out.println("数组大小不能初始化为小于0的空间");
        }
    }

    /**
     * 数组扩容
     */
    private void extendSize(){

        int[] extendArray = new int[this.size * 2];

        for (int i = 0; i < this.size; i++) {
            extendArray[i] = this.data[i];
        }

        this.data = extendArray;
        this.size = this.data.length;
    }

    /**
     * 数组输出
     */
    private void print(){

        System.out.println("=============");
        for (int i = 0; i < this.size; i++) {
            System.out.print(this.data[i] + " ");
        }
        System.out.println();
    }

    /**
     * 查询数组某个位置的值
     * @param index
     * @return
     */
    private Integer query(int index) {

        if(index < this.size) {
            return this.data[index];
        }

        return null;
    }

    /**
     * 更新数组某个位置的值
     * @param index
     * @param toUpdate
     * @return
     */
    private void update(int index, int toUpdate) {

        if(index < this.data.length) {
            this.data[index] = toUpdate;
        } else {
            System.out.println("同学 你越界了!!!");
        }

    }

    /**
     * 添加数组注意扩容
     * @param loc
     * @param insertData
     * @return
     */
    private void insert(int loc, int insertData){

        //越界先扩容
        if(this.index++ >= this.size || loc >= this.size || this.data[size-1] != 0) {
            this.extendSize();
        }

        for (int i = this.size - 1; i > loc; i--) {
            this.data[i] = this.data[i-1];
        }

        this.data[loc] = insertData;
    }

    /**
     * 删除数组注意越界问题
     * @param loc
     */
    private void delete(int loc){

        if(loc >= 0 && loc < this.size) {
            for (int i = loc; i < this.size; i++) {
                if(i != this.size - 1){ //防止越界
                    this.data[i] = this.data[i + 1];
                } else {
                    this.data[i] = 0;
                }
            }
        } else {
            System.out.println("同学 你越界了!!!");
        }

        this.index--;
    }

    public static void main(String[] args) {

        ArrayImpl array = new ArrayImpl();
        array.init(10);
        array.update(0,1);
        array.update(2,2);
        array.update(4,3);
        array.update(5,4);
        array.update(6,5);
        array.update(8,6);
        array.update(9,7);
        array.print();
        array.insert(2, 12);
        array.print();
        array.insert(12, 100);
        array.print();
        array.delete(2);
        array.print();
        array.update(10, 51);
        array.print();
    }

}


---------print----------

=============
1 0 2 0 3 4 5 0 6 7 
=============
1 0 12 2 0 3 4 5 0 6 7 0 0 0 0 0 0 0 0 0 
=============
1 0 12 2 0 3 4 5 0 6 7 0 100 0 0 0 0 0 0 0 
=============
1 0 2 0 3 4 5 0 6 7 0 100 0 0 0 0 0 0 0 0 
=============
1 0 2 0 3 4 5 0 6 7 51 100 0 0 0 0 0 0 0 0 

Process finished with exit code 0

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值