JAVA数据结构--ArrayList动态数组
在计算机科学中,动态数组,可扩展数组,可调整数组,动态表,可变数组或数组列表是一种随机存取可变大小列表数据结构,允许添加或删除元素。它提供许多现代主流编程语言的标准库。动态数组克服了静态数组的限制,静态数组具有需要在分配时指定的固定容量。
动态数组与动态分配的数组不同,数组是数组分配时大小固定的数组,尽管动态数组可能使用固定大小的数组作为后端。
代码实现:
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动态数组的更多相关文章
- ArrayList类源码解析——ArrayList动态数组的实现细节(基于JDK8)
一.基本概念 ArrayList是一个可以添加对象元素,并进行元素的修改查询删除等操作的容器类.ArrayList底层是由数组实现的,所以和数组一样可以根据索引对容器对象所包含的元素进行快速随机的查询 ...
- Java数据结构ArrayList
Java数据结构ArrayList /** * <html> * <body> * <P> Copyright JasonInternational</p&g ...
- (2)redis的基本数据结构是动态数组
redis的基本数据结构是动态数组 一.c语言动态数组 先看下一般的动态数组结构 struct MyData { int nLen; ]; }; 这是个广泛使用的常见技巧,常用来构成缓冲区.比起指针, ...
- Java数据结构和算法 - 数组
Q: 数组的创建? A: Java中有两种数据类型,基本类型和对象类型,在许多编程语言中(甚至面向对象语言C++),数组也是基本类型.但在Java中把数组当做对象来看.因此在创建数组时,必须使用new ...
- C#深入研究ArrayList动态数组自动扩容原理
1 void Test1() { ArrayList arrayList = new ArrayList(); ; ; i < length; i++) { arrayList.Add(&quo ...
- python数据结构之动态数组
数组列表:动态数组(Array List) 简介: 最基础简单的数据结构.最大的优点就是支持随机访问(O(1)),但是增加和删除操作效率就低一些(平均时间复杂度O(n)) 动态数组也称数组列表,在py ...
- java——时间复杂度、动态数组
O(n)不一定小于O(n^2),要具体来看,而我们说的这种时间复杂度其实是渐进时间复杂度,描述的是n趋近于无穷的情况. 动态数组的时间复杂度: 添加操作:O(n) addLast()的均摊复杂度为O( ...
- nginx学习七 高级数据结构之动态数组ngx_array_t
1 ngx_array_t结构 ngx_array_t是nginx内部使用的数组结构.nginx的数组结构在存储上与大家认知的C语言内置的数组有相似性.比方实际上存储数据的区域也是一大块连续的内存. ...
- Java中ArrayList与数组间相互转换
在实际的 Java 开发中,如何选择数据结构是一个非常重要的问题. 衡量标准化(读的效率与改的效率) : ① Array: 读快改慢 ② Linked :改快读慢 ③ Hash:介于两者之间 实现Li ...
随机推荐
- linux系统上部署一个web项目
对于apache开源项目中tomcat的认识,大多停留在Windows下,这次我通过一个简单的实例来介绍一下在linux下如何搭建tomcat环境,并且部署一个web项目. 先从基本安装开始,可别小看 ...
- Spring中使用Velocity模板
使用Velocity模板 Velocity是一种针对Java应用的易用的模板语言.Velocity模板中没有任何 Java代码,这使得它能够同时被非开发人员和开发人员轻松地理解.Velocity的用户 ...
- Solidity mapping循环
https://medium.com/@blockchain101/looping-in-solidity-32c621e05c22
- 关于box-sizing属性
写在前面 文中错误或不足之处欢迎指正批评,共同交流! 在项目中写css组件时遇到一个问题: 要求两个按钮均分其父元素宽度,且父元素宽度不固定,像这样: 第一反应很自然的想到使用flex布局,但是由于需 ...
- 9.hive聚合函数,高级聚合,采样数据
本文主要使用实例对Hive内建的一些聚合函数.分析函数以及采样函数进行比较详细的讲解. 一.基本聚合函数 数据聚合是按照特定条件将数据整合并表达出来,以总结出更多的组信息.Hive包含内建的一些基本聚 ...
- javascript总结17:javascript 函数简介
1 释义:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 2 格式:通过 function 关键字. function test(){ alert("您好"); } ...
- HTTP文件上传插件开发文档-ASP
版权所有 2009-2016 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-u ...
- Java 可变字符串StringBuilder/StringBuffer的区别
public class StringBuilder_and_StringBuffer { private static long SystemTime(){ return System.curren ...
- PS插件开发plugin
Photoshop插件开发 VC++制作Photoshop自动化插件:http://blog.sina.com.cn/s/blog_73c52fda0101c7hw.html Photoshop 的扩 ...
- Displaying Speed and Direction Symbology from U and V vectors (转)
This blog shows you how to calculate and symbolize wind or current speed and direction when the unde ...