美文网首页c/c++
c++基础-实现 Native 层的 ArrayList

c++基础-实现 Native 层的 ArrayList

作者: Peakmain | 来源:发表于2019-02-24 16:52 被阅读1次

c++中模板类的话,需要将模板类和实现类放在同一个类中,也就是需要新建名为.hpp的文件,对于java的ArrayList源码分析可以看这篇文章https://www.jianshu.com/p/d4b2491a728b

#include "malloc.h"

//-------------------------------声明------------------------------//
template<class E>
class ArrayList {
private:
    E *array = NULL;
    //数组的长度
    int len = 0;
    //当前角标
    int index = 0;
public:
    ArrayList();

    ArrayList(int capacity);

    void add(E e);

    E remove(int index);

    E get(int index);

    ~ArrayList();

    int size();

private:
    void ensureCapacityInternal(int capacity);

    void grow(int capacity);
};

//-------------------------------实现------------------------------//
template<class E>
ArrayList<E>::ArrayList() {


};

template<class E>
ArrayList<E>::ArrayList(int capacity) {
    if (capacity < 0) {
        return;
    }
    this->len = capacity;
    this->array = (E *) malloc(sizeof(E));

};

template<class E>
ArrayList<E>::~ArrayList() {
    if (this->array) {
        free(this->array);
        this->array = NULL;
    }
};

template<class E>
int ArrayList<E>::size() {
    return this->index;
}

template<class E>
void ArrayList<E>::add(E e) {
    ensureCapacityInternal(index + 1);  // Increments modCount!!
    this->array[index++] = e;
}

template<class E>
void ArrayList<E>::ensureCapacityInternal(int capacity) {
    if (this->array == NULL) {
        capacity = 10;
    }
    if (capacity - len > 0) {
        //开辟新的数组
        grow(capacity);
    }
}

//扩容
template<class E>
void ArrayList<E>::grow(int capacity) {
    int new_len = len + (len >> 1);
    if (capacity > new_len) {
        new_len = capacity;
    }
    //创建新的数组
    E *newArray = (E *) malloc(sizeof(E) * new_len);
    if (this->array) {
        //原来的数据拷贝
        memcpy( newArray,this->array, sizeof(E) * index);//sizeof(E)拷贝的字节
        free(this->array);//防止内存泄露
    }
    this->array = newArray;
    this->len = new_len;
}
template<class E>
E ArrayList<E>::get(int index){
    return this->array[index];
}
template<class E>
E ArrayList<E>::remove(int index) {
    E old_value = this->array[index];
    // 计算出需要逻动的个数
    int numMoved = this->index - index - 1;

    // 从前面不断的逻动
    for (int i = 0; i < numMoved; ++i) {
        array[index + i] = array[index + i + 1];
    }

    this->index -= 1;
    return old_value;
}
#endif //NDK_ARRAYLIST_H

相关文章

  • C++基础 - 实现 Native 层的 ArrayList

    ArrayList 我们在 java 中再熟悉不过了,记得自己在学习 Collection 体系的时候,用得最多的...

  • C++基础 - 实现 Native 层的 ArrayList

    ArrayList 我们在 java 中再熟悉不过了,记得自己在学习 Collection 体系的时候,用得最多的...

  • c++基础-实现 Native 层的 ArrayList

    c++中模板类的话,需要将模板类和实现类放在同一个类中,也就是需要新建名为.hpp的文件,对于java的Array...

  • Android NDK模拟native crash

    最近需要模拟出一个native crash,简单来说就是声明一个native方法,然后在c/c++层实现这个方法并...

  • NDK 音视频的直播推流与流媒体播放

    Java层的native方法和C/C++层的函数建立对应关系有两种方式: 静态注册Java 层的 native 方...

  • [019]JNI基础

    前言 如果要对android系统有一个深入的理解,Native层C/C++是代码是必看的,Binder的底层实现,...

  • JNI 原理

    我们都知道JNI结构是 Java 层 -> JNI -> Native 层, 以此实现Java 层和Native层...

  • java native

    native说明当前函数不是用java实现,而是通过其他语言(如C/C++)实现,用JNI(Java Native...

  • Android知识点汇总

    Java基础 谈谈 ArrayList 和 LinkList 的区别 ArrayList是实现了基于动态数组的数据...

  • java基础之关键字

    native native是用做java和其他语言(如c++)进行协作时用的 也就是native后的函数的实现不是...

网友评论

    本文标题:c++基础-实现 Native 层的 ArrayList

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