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

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

  

  代码实现:

 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. 优化tomcat配置(从内存、并发、缓存3个方面)优化

    Tomcat有很多方面,我从内存.并发.缓存三个方面介绍优化方法. 一.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catal ...

  2. CURL以 POST 请求链接的方式 初始化一个cURL会话来获取一个网页

    /** *POST URL */ function posturl($URL,$data) { $ch = curl_init(); // 创建一个新cURL资源 curl_setopt($ch,CU ...

  3. 2014蓝桥杯B组初赛试题《李白打酒》

    题目描述: 话说大诗人李白,一生好饮.幸好他从不开车.     一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒.     逢店加一倍,遇花喝一斗.     这一路上 ...

  4. ObjectMapper对象的使用 Object2JSON

    // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...

  5. linux下的同步与互斥

    linux下的同步与互斥 谈到linux的并发,必然涉及到线程之间的同步和互斥,linux主要为我们提供了几种实现线程间同步互斥的 机制,本文主要介绍互斥锁,条件变量和信号量.互斥锁和条件变量包含在p ...

  6. sencha:日期选择组件datepicker

    来源于<sencha touch权威指南> ------------------------------- 除app.js外,其它内容都与上一篇博客里的内容相同.app.js代码如下: E ...

  7. 487C Prefix Product Sequence

    传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...

  8. 打印单据,A4纸,每个单据占一个A4纸,两个单据之间不挨着

    打印单据,A4纸,每个单据占一个A4纸,两个单据之间不挨着 <style type="text/css" media="print">.Noprin ...

  9. PHP中static与self

    一直搞不清楚,今天百度自己也测试了一下. <?php class A { public static function closure(){ echo __CLASS__."<b ...

  10. Django框架 之 查询 Extra

    Django框架 之 查询 Extra extra 1 2 extra(select=None, where=None, params=None,       tables=None, order_b ...