功能:

1.增、删、改、查

2.扩容、缩容

3.复杂度分析

4.均摊复杂度

5.复杂度震荡


分析动态数组的时间复杂度:


分析resize的时间复杂度:


public class Array<E> {

    private E[] data;
private int size; // 构造函数,传入数组的容量capacity构造Array
public Array(int capacity){
data = (E[])new Object[capacity];
size = 0;
} // 无参数的构造函数,默认数组的容量capacity=10
public Array(){
this(10);
} // 获取数组的容量
public int getCapacity(){
return data.length;
} // 获取数组中的元素个数
public int getSize(){
return size;
} // 返回数组是否为空
public boolean isEmpty(){
return size == 0;
} // 在index索引的位置插入一个新元素e
public void add(int index, E e){ if(index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size."); if(size == data.length)
resize(2 * data.length); for(int i = size - 1; i >= index ; i --)
data[i + 1] = data[i]; data[index] = e; size ++;
} // 向所有元素后添加一个新元素
public void addLast(E e){
add(size, e);
} // 在所有元素前添加一个新元素
public void addFirst(E e){
add(0, e);
} // 获取index索引位置的元素
public E get(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed. Index is illegal.");
return data[index];
} // 修改index索引位置的元素为e
public void set(int index, E e){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Index is illegal.");
data[index] = e;
} // 查找数组中是否有元素e
public boolean contains(E e){
for(int i = 0 ; i < size ; i ++){
if(data[i].equals(e))
return true;
}
return false;
} // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(E e){
for(int i = 0 ; i < size ; i ++){
if(data[i].equals(e))
return i;
}
return -1;
} // 从数组中删除index位置的元素, 返回删除的元素
public E remove(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed. Index is illegal."); E ret = data[index];
for(int i = index + 1 ; i < size ; i ++)
data[i - 1] = data[i];
size --;
data[size] = null; // loitering objects != memory leak if(size == data.length / 4 && data.length / 2 != 0)
resize(data.length / 2);
return ret;
} // 从数组中删除第一个元素, 返回删除的元素
public E removeFirst(){
return remove(0);
} // 从数组中删除最后一个元素, 返回删除的元素
public E removeLast(){
return remove(size - 1);
} // 从数组中删除元素e
public void removeElement(E e){
int index = find(e);
if(index != -1)
remove(index);
} @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();
} // 将数组空间的容量变成newCapacity大小
private void resize(int newCapacity){ E[] newData = (E[])new Object[newCapacity];
for(int i = 0 ; i < size ; i ++)
newData[i] = data[i];
data = newData;
}
}

封装动态数组类Array的更多相关文章

  1. C++学习之动态数组类的封装

    动态数组(Dynamic Array)是指动态分配的.可以根据需求动态增长占用内存的数组.为了实现一个动态数组类的封装,我们需要考虑几个问题:new/delete的使用.内存分配策略.类的四大函数(构 ...

  2. 模仿.NET框架ArrayList写一个自己的动态数组类MyArrayList,揭示foreach实现原理

    通过.NET反编译工具可以查看到ArrayList内部的代码,发现ArrayList并非由链表实现,而是由一个不断扩容的数组对象组成. 下面模仿ArrayList写一个自己的MyArrayList. ...

  3. DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别

    在应用中发现静态数组和动态数组是有区别的: procedure TForm1.Button1Click(Sender: TObject);var  RsltStream: TMemoryStream; ...

  4. 【java集合总结】-- 数组总结+自己封装数组类

    一.前言 本篇文章总结目前学习的有关数组方面的知识,首先总结一下数组相关的核心概念,然后在封装一个自己的泛型动态数组类(ava已经封装的有现成的,自己封装只是为了加深理解),最后再学习解析下Array ...

  5. 一篇文章让你了解动态数组的数据结构的实现过程(Java 实现)

    目录 数组基础简单回顾 二次封装数组类设计 基本设计 向数组中添加元素 在数组中查询元素和修改元素 数组中的包含.搜索和删除元素 使用泛型使该类更加通用(能够存放 "任意" 数据类 ...

  6. 用最复杂的方式学会数组(Python实现动态数组)

    Python序列类型 在本博客中,我们将学习探讨Python的各种"序列"类,内置的三大常用数据结构--列表类(list).元组类(tuple)和字符串类(str). 不知道你发现 ...

  7. C++——模板、数组类

    1.函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 声明方法:template<typename 标识符> 函数声明 求绝对值的模板 #in ...

  8. 动态数组java实现

    数组是一种顺序存储的线性表,所有元素的内存地址是连续的. 动态数组相对于一般数组的优势是可以灵活地添加或删除元素.而一般数组则受限于固定的内存空间.只能有限的添加元素 动态数组(Dynamic Arr ...

  9. 算法入门 - 动态数组的实现(Java版本)

    静态数组 Java中最基本的数组大家肯定不会陌生: int[] array = new int[6]; for (int i = 0; i < array.length; i++){ array ...

随机推荐

  1. 微信小程序获取数据、处理数据、绑定数据关键步骤记录

    onload:function(event){ var inTheatersUrl ="https://api.douban.com"+"/v2/movie/in_the ...

  2. WCF、WebAPI、WCFREST、WebService 、RPC、HTTP 概念解释

    在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...

  3. 微信公众号自动回复_Java

    先声明一下,这是一个maven工程pom文件需要的依赖: <dependency> <groupId>dom4j</groupId> <artifactId& ...

  4. 在CentOS上配置MySQL服务

    #!/bin/sh # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB # Thi ...

  5. 减少服务器压力php生成静态xml文件

    一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...

  6. webpack-dev-server.js 服务器配置说明

    connect-history-api-fallback 使用: var app = express() var histroy = require('connect-history-api-fall ...

  7. 当尝试从ArcCatalog、.net应用或是Java应用中连接ArcGIS Server 时,显示下面任何一种错误提示: "Access Denied" 或 "The connection could not be made"

    Error: 访问拒绝或无法连接错误 文章编号 : 29042 软件: ArcGIS Server 9.0, 9.1, 9.2, 9.3, 9.3.1 操作系统: Windows 2000, XP, ...

  8. time和datetime模块

    在Python中,通常有这几种方式来表示时间: 1)时间戳 2)格式化的时间字符串  3)元组(struct_time)共九个元素. 由于Python的time模块实现主要调用C库,所以各个平台可能有 ...

  9. 将caj转换成pdf

    1.工具准备 电脑一台 CAJViewer 7.2 foxit pdf reader [主是要拥有一个pdf的虚拟打印机,你也可以安装其他的可以取的pdf虚拟打印机的软件.] 2.步骤 (1)用CAJ ...

  10. Do not set "root" as "NOPASSWD" in sudoers file

    cat /etc/sudoers root    ALL=(ALL)ALL: ALL do not change it to root    ALL=(ALL)NOPASSWD: ALL Since ...