在计算机科学中,动态数组,可扩展数组可调整数组动态表可变数组数组列表是一种随机存取可变大小列表数据结构,允许添加或删除元素。它提供许多现代主流编程语言的标准库。动态数组克服了静态数组的限制,静态数组具有需要在分配时指定的固定容量。

动态数组与动态分配的数组不同,数组是数组分配时大小固定的数组,尽管动态数组可能使用固定大小的数组作为后端。

  

  代码实现:

 package DataStructures;

 import java.util.Iterator;
import java.util.NoSuchElementException; public class MyArrayList<AnyType> implements Iterable<AnyType> { private static final int DEFAULT_CAPACITY=10;
private int theSize;
private AnyType[] theItems;
public MyArrayList() {
doClear();
// TODO Auto-generated constructor stub
}
private void clear(){
doClear();
}
private void doClear(){ //移除动态数组中所有元素
theSize=0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size(){ //返回当前动态数组大小
return theSize;
}
public boolean isEmpty(){
return size()==0;
}
public void trimToSize(){ //将动态数组的容量调整为当前实例的大小
ensureCapacity(size());
}
public AnyType get(int idx){
if(idx<0||idx>=size())
throw new ArrayIndexOutOfBoundsException();
return theItems[idx];
}
public AnyType set(int idx,AnyType newVal){
if(idx<0||idx>=size())
throw new ArrayIndexOutOfBoundsException();
AnyType old =theItems[idx];
theItems[idx]=newVal;
return old;
}
private void ensureCapacity(int newCapacity) { //为当前的动态数组扩容
if(newCapacity<theSize)
return;
AnyType[] old=theItems;
theItems=(AnyType []) new Object[newCapacity];
for(int i=0;i<size();i++)
theItems[i]=old[i];
}
public boolean add(AnyType x){
add(size(),x);
return true;
}
public void add(int idx,AnyType 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 AnyType remove(int idx){
AnyType removeItem=theItems[idx];
for(int i=idx;i<size()-1;i++)
theItems[i]=theItems[i+1];
theSize--;
return removeItem;
}
@Override
public Iterator<AnyType> iterator() { //迭代器iterator方法返回的是ArrayListIterator的一个实例
// TODO Auto-generated method stub
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator<AnyType>{
private int current=0;
public boolean hasNext(){
return current<size();
}
public AnyType next(){
if(!hasNext())
throw new NoSuchElementException();
return theItems[current++];
}
public void remove(){
MyArrayList.this.remove(--current);
}
}
}

JAVA数据结构--ArrayList动态数组的更多相关文章

  1. ArrayList类源码解析——ArrayList动态数组的实现细节(基于JDK8)

    一.基本概念 ArrayList是一个可以添加对象元素,并进行元素的修改查询删除等操作的容器类.ArrayList底层是由数组实现的,所以和数组一样可以根据索引对容器对象所包含的元素进行快速随机的查询 ...

  2. Java数据结构ArrayList

    Java数据结构ArrayList /** * <html> * <body> * <P> Copyright JasonInternational</p&g ...

  3. (2)redis的基本数据结构是动态数组

    redis的基本数据结构是动态数组 一.c语言动态数组 先看下一般的动态数组结构 struct MyData { int nLen; ]; }; 这是个广泛使用的常见技巧,常用来构成缓冲区.比起指针, ...

  4. Java数据结构和算法 - 数组

    Q: 数组的创建? A: Java中有两种数据类型,基本类型和对象类型,在许多编程语言中(甚至面向对象语言C++),数组也是基本类型.但在Java中把数组当做对象来看.因此在创建数组时,必须使用new ...

  5. C#深入研究ArrayList动态数组自动扩容原理

    1 void Test1() { ArrayList arrayList = new ArrayList(); ; ; i < length; i++) { arrayList.Add(&quo ...

  6. python数据结构之动态数组

    数组列表:动态数组(Array List) 简介: 最基础简单的数据结构.最大的优点就是支持随机访问(O(1)),但是增加和删除操作效率就低一些(平均时间复杂度O(n)) 动态数组也称数组列表,在py ...

  7. java——时间复杂度、动态数组

    O(n)不一定小于O(n^2),要具体来看,而我们说的这种时间复杂度其实是渐进时间复杂度,描述的是n趋近于无穷的情况. 动态数组的时间复杂度: 添加操作:O(n) addLast()的均摊复杂度为O( ...

  8. nginx学习七 高级数据结构之动态数组ngx_array_t

    1 ngx_array_t结构 ngx_array_t是nginx内部使用的数组结构.nginx的数组结构在存储上与大家认知的C语言内置的数组有相似性.比方实际上存储数据的区域也是一大块连续的内存. ...

  9. Java中ArrayList与数组间相互转换

    在实际的 Java 开发中,如何选择数据结构是一个非常重要的问题. 衡量标准化(读的效率与改的效率) : ① Array: 读快改慢 ② Linked :改快读慢 ③ Hash:介于两者之间 实现Li ...

随机推荐

  1. 【总结整理】pv、uv

    1.pv的全称是page view,译为页面浏览量或点击量,通常是衡量一个网站甚至一条网络新闻的指标.用户每次对网站中的一个页面的请求或访问均被记录1个PV,用户对同一页面的多次访问,pv累计.例如, ...

  2. 【项目运行异常】BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking ...

  3. 什么是Kali Linux?

    什么是Kali Linux? Kali Linux是一个基于Debian的Linux发行版,旨在实现高级渗透测试和安全审计.Kali包含数百种工具,适用于各种信息安全任务,如渗透测试,安全研究,计算机 ...

  4. R 如何 隐藏坐标轴

    x = c(7,5,8)dim(x)<-3names(x)<-c("apple","banana", "cherry")plot ...

  5. 关于Tomcat中封装请求-响应的结构的分析

    在编写Servlet时,往往只重写了doGet和doPost方法,使用Tomcat通过(HttpServletRequest 和 HttpServletResponse)接口传递来的request和r ...

  6. SQL语句学习积累·数据的操作

    数据的操作 select 取表中前五条数据 select top 5 from table_name 取表中前50%的数据 select top 50 percent from table_name ...

  7. 现代C++学习笔记之一入门篇:智能指针(C++ 11)

    原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 在现代 C++ 编程中,标准库包含智能指针,该指针用于确保 ...

  8. 20169219 SEED SQL注入实验

    实验环境SEED Ubuntu镜像 环境配置 实验需要三样东西,Firefox.apache.phpBB2(镜像中已有): 1.运行Apache Server:只需运行命令sudo service a ...

  9. C++程序的目录结构、编译、打包、分发

    管理C++的第三方库以及编译 第三方库这个说法,不知道出自哪里,但一般是指开发者,系统/平台提供商之外的第三个参与者提供的程序库. 大多数开源软件库在软件系统中都是第三方库. 完全不使用库的开发,在9 ...

  10. php复制目录

    function copyDir($dirSrc,$dirTo) { if(is_file($dirTo)) { echo '目标不是目录不能创建!'; return; } if(!file_exis ...