美文网首页数据程序视界
[图解数据结构之Java实现](1) --- 线性表之数组实现

[图解数据结构之Java实现](1) --- 线性表之数组实现

作者: 梦蓝樱飞2020 | 来源:发表于2017-05-06 21:04 被阅读258次

    本文行文思路结构

    一. 线性表
    二. 数组
    三. 表的简单数组实现
    四. 表的各种基本操作 --- 增删改查
        1. 图解分析
        2. 代码实现
    五. 完整测试代码
    六. 总结
    

    一. 线性表

    线性结构是数据结构中三种基本结构之一. 而线性结构的特点是:
    在数据元素的非空有限集合中

    1. 存在唯一的一个被称为"第一个"的数据元素;
    2. 存在唯一的一个被称为"最后一个"的数据元素;
    3. 除第一个之外, 集合中的每个数据元素均只有一个前驱;
    4. 除最后一个之外, 集合中的每个数据元素均只有一个后继.

    同时线性结构中包括常见的数据结构:

    • 线性表
    • 队列

    今天, 就来重点介绍最常用且最简单的一种数据结构 --- 线性表, 简言之, 一个线性表是n个数据元素的有限序列.
    而实现线性表的方式一般有两种,一种是使用数组存储线性表的元素,即用一组连续的存储单元依次存储线性表的数据元素。
    另一种是使用链表存储线性表的元素,即用一组任意的存储单元存储线性表的数据元素(存储单元可以是连续的,也可以是不连续的)。
    这两种方式就对应两种不同的存储结构: 顺序存储结构链式存储结构.

    二. 数组

    以下是维基百科的定义:

    在计算机科学中,数组数据结构(英语:array data structure),简称数组(英语:Array),是由相同类型的元素(element)的集合所组成的数据结构,分配一块连续的内存来存储。利用元素的索引(index)可以计算出该元素对应的储存地址。

    其实, 数组是应用最广泛的一种数据结构, 常常被植入到编程语言中,作为基本数据类型来使用. 简言之, 数组就是将元素在内存中连续存放,由于每个元素占用内存相邻,可以通过下标迅速访问数组中任何元素。
    以一维数组为例, (当然二维数组实际上也是一维数组)
    int[] arr = new int[3];

    数组在内存中的结构.PNG

    三. 表的简单数组实现

    数组是一种大小固定的数据结构, 虽然数组是由固定容量创建的, 但在需要的时候可以用加倍的容量(一般是双倍)来重新创建一个不同的数组, 同时对表的所有操作都可以通过使用数组来实现.

    int[] arr = new int[10];
    //...
    // 扩大arr
    int[] newArr = new int[arr.length * 2];
    for (int i = 0; i < arr.length; i++)
        newArr[i] = arr[i];
    arr = newArr;
    

    四. 表的各种基本操作 --- 增删改查

    线性表的数组实现, 正好对应于JDK集合框架中的ArrayList, 现自定义一个类MyArrayList.

    public class MyArrayList<E> {
            // 静态变量DEFAULT_CAPACITY, 代表初始数组默认大小
        private static final int DEFAULT_CAPACITY = 10;
        
            // 线性表当前大小
        private int theSize;
        private E [] theItems;
            // 类中的成员方法省略
    }
    

    添加元素

    增.png

    对应代码:

    
        public boolean add(E x) {
            add(size(), x);
            return true;
        }
        public void add(int idx, E x) {
            if (theItems.length == size()) {
                ensureCapacity(size() * 2 + 1);
            }
            for (int i = theSize; i > idx; i--) {
                theItems[i] = theItems[i - 1];
            }
            theItems[idx] = x;
            
            theSize++;
        }
    
    

    删除元素

    删.png

    对应代码:

        public E remove(int idx) {
            E removedItem = theItems[idx];
            for (int i = idx; i < size() - 1; i++) {
                theItems[i] = theItems[i + 1];
            }
            theSize--;
            return removedItem; 
        }
    

    修改元素

    改.png

    对应代码:

        public E set(int idx, E newVal) {
            if (idx < 0 || idx >= size()) {
                throw new ArrayIndexOutOfBoundsException();
            }
            E old = theItems[idx];
            theItems[idx] = newVal;
            return old;
        }
    

    查找元素

    查.png

    对应代码:

        public E get(int idx) {
            if (idx < 0 || idx >= size()) {
                throw new ArrayIndexOutOfBoundsException();
            }
            return theItems[idx];
        }
    

    五. 完整测试代码

    线性表的实现
    MyArrayList.java

    package org.lxy.ds;
    /**
     * 自定义的ArrayList
     * @author menglanyingfei
     * @date 2017-5-6
     */
    public class MyArrayList<E> {
        // 静态变量DEFAULT_CAPACITY, 代表初始数组默认大小
        private static final int DEFAULT_CAPACITY = 6;
        
        // 线性表当前大小
        private int theSize;
        // 实现该线性表的数组
        private E [] theItems;  
        
        // 无参构造
        public MyArrayList () {
            clear();
        }
        // 初始化
        public void clear() {
            theSize = 0;
            ensureCapacity(DEFAULT_CAPACITY);
        }
        // 确保theSize<=DEFAULT_CAPACITY
        @SuppressWarnings("unchecked")
        public void ensureCapacity(int newCapacity) {
            if (newCapacity < theSize) {
                return;
            }
            // 中间变量, 用来存之前数组里的值
            E [] old = theItems;
            // 强制类型转换
            theItems = (E[]) new Object[newCapacity]; 
            for (int i = 0; i < size(); i++) {
                theItems[i] = old[i];
            }
        }
        // 返回线性表当前大小
        public int size() { 
            return theSize;
        }
        // 增
        public boolean add(E x) {
            add(size(), x);
            return true;
        }
        public void add(int idx, E x) {
            if (theItems.length == size()) {
                ensureCapacity(size() * 2 + 1);
            }
            for (int i = theSize; i > idx; i--) {
                theItems[i] = theItems[i - 1];
            }
            theItems[idx] = x;
            
            theSize++;
        }
        // 删
        public E remove(int idx) {
            E removedItem = theItems[idx];
            for (int i = idx; i < size() - 1; i++) {
                theItems[i] = theItems[i + 1];
            }
            theSize--;
            return removedItem; 
        }
        // 改
        public E set(int idx, E newVal) {
            if (idx < 0 || idx >= size()) {
                // 抛出数组索引越界异常
                throw new ArrayIndexOutOfBoundsException();
            }
            E old = theItems[idx];
            theItems[idx] = newVal;
            return old;
        }
        // 查
        public E get(int idx) {
            if (idx < 0 || idx >= size()) {
                throw new ArrayIndexOutOfBoundsException();
            }
            return theItems[idx];
        }
        // 显示线性表中的数据
        public void show() {
            for (int i = 0; i < size(); i++) {
                System.out.print(theItems[i] + " ");
            }
        }
    }
    

    功能测试
    MyArrayListTest.java

    package org.lxy.ds;
    
    import org.lxy.ds.MyArrayList;
    
    /**
     * @author menglanyingfei
     * @date 2017-5-6
     */
    public class MyArrayListTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // 创建线性表对象
            MyArrayList<Integer> myArrayList = new MyArrayList<>();
            // 第一种添加元素方式, add(E x)
            myArrayList.add(1); // 1
            myArrayList.add(2); // 1 2
            //myArrayList.add(5);
            
            // 第二种添加元素方式, add(int idx, E x)
            myArrayList.add(0, 5); // 5 1 2
            
            myArrayList.remove(1); // 5 2
            
            myArrayList.set(0, 6); // 6 2
            
            System.out.println(myArrayList.get(0));
            
            myArrayList.show();
        }
    }
    

    六. 总结

    到此, 所有的内容就总结完了! 用数组实现, 其实相对比较简单, 只要注意添加和删除元素, 需要移动其它元素就行!

    最后, 非常欢迎各位小伙伴评论和指点我的文章, 如果您觉得写得还不太差劲或者对您有一丁点的帮助, 麻烦动个小手点个赞, 好人萌萌哒, 也很感谢您耐心认真地看完!

    相关文章

      网友评论

        本文标题:[图解数据结构之Java实现](1) --- 线性表之数组实现

        本文链接:https://www.haomeiwen.com/subject/pytytxtx.html