破壳Ai

  • 编程入门
    • C语言
    • 数据结构及算法基础
  • 应用程序开发
    • C++
    • Windows API编程
    • opengl图形编程
  • 计算机科学与Web
    • Python
    • 编程语言学
    • 信息理论
    • 人工智能
  • 计算机原理
    • 汇编
    • 系统编程
    • 操作系统
    • 编译原理
    • 网络
  • 关于
    • 关于
    • 友链
    • 支持小站
    • 留言
忘记时间 专注当下
  1. 首页
  2. 编程入门
  3. 数据结构及算法基础
  4. 正文

漫画算法之『数组』-Java演示

2020-11-26 683点热度 1人点赞 0条评论

寻找志同道合的学习伙伴,请访问我的个人网页.
该内容同步发布在CSDN和耳壳网.

提示

数组的插入、扩容、删除等操作

/**
 * 数组 - 基本操作
 * @author Guyuehu
 *
 */
public class Main {
    public static void main(String[] args) throws Exception {
        MyArray arrayTest = new MyArray(4);
        arrayTest.insert(0, 6);
        arrayTest.insert(1, 10);
        arrayTest.insert(2, 0);
        arrayTest.insert(3, 7);
        //arrayTest.insert(1, 100);
        arrayTest.insertOutBounds(1, 100);
        arrayTest.delete(2);
        arrayTest.delete(3);
        arrayTest.output();
    }
}

/**
 * 创建数组类
 * @author Guyuehu
 *
 */
class MyArray {
    private int[] array;
    private int size;

    public MyArray(int capacity) {
        this.array = new int[capacity];
        this.size = 0;
    }

    /**
     * 从数组中间插入元素
     * @param  index    插入元素位置
     * @param  element  插入的元素
     */
    public void insert(int index, int element) throws Exception {
        //超出数组范围抛出异常
        //若index > size 插入的元素就不是顺序放在数组中了
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("超出数组实际元素范围!");
        }
        //从后往前直到inedex位置依次往后移一位
        for(int i = size - 1; i >= index; i--) {
            array[i+1] = array[i];
        }
        //插入
        array[index] = element;
        size++;
    }

    /**
     *超范围插入
     */
    public void insertOutBounds(int index, int element) throws Exception {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("超出数组实际元素范围!");
        }
        //数组已满,则对数组扩容
        if(size >= array.length) {
            resize();
        }
        //插入
        for(int i = size - 1; i >= index; i--) {
            array[i+1] = array[i];
        }
        //插入
        array[index] = element;
        size++;
    }

    /**
     * 数组扩容
     */
    public void resize() {
        int[] arrayNew = new int[2 * array.length];
        System.arraycopy(array, 0, arrayNew, 0, array.length);
        array = arrayNew;
    }

    /**
     * 数组删除元素
     * @param  index    删除元素位置
     */
    public int delete(int index) throws Exception {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("超出数组实际元素范围!");
        }
        int deleteElement = array[index];
        for(int i = index; i < size - 1; i++) {
            array[i] = array[i+1];
        }
        //size-- 很重要,时刻保持与现存数组元素个数等长  
        size--;
        return deleteElement;
    }

    /**
     * 输出数组
     */
    public void output() {
        for(int i = 0; i < size; i++) {
            System.out.println(array[i]);
        }
    }

}

寻找志同道合的学习伙伴,请访问我的个人网页.
该内容同步发布在CSDN和耳壳网.

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Java
最后更新:2020-11-26

Guyue

这个人很懒,什么都没留下

打赏 点赞
下一篇 >

文章评论

取消回复

关注公众号
Donation

Yahaha,you found me!

文章目录
  • 提示

COPYRIGHT © 2019-2022 破壳AI. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备19036001号