ArrayList数据结构的实现
import java.util.Iterator;
import java.util.NoSuchElementException; public class MyArrayList<T> implements Iterable<T> {
//默认数组大小
private static final int DEFAULT_CAPACITY=;
//表大小
private int theSize;
//数组存储
private T[] theItems;
//初始化表
public MyArrayList(){
doClear();
}
private void doClear() {
// TODO Auto-generated method stub
theSize=;
ensureCapacity(DEFAULT_CAPACITY);
}
//清空表
public void Clear(){
doClear();
}
//判断是否为空
public boolean isEmpty(){
return size()==;
}
//将此 ArrayList 实例的容量调整为列表的当前大小
public void trimToSize(){
ensureCapacity(size());
} public T get(int index){
if(index<||index>size()){
throw new ArrayIndexOutOfBoundsException();
}
return theItems[index];
}
public T set(int index,T val){
if(index<||index>size()){
throw new ArrayIndexOutOfBoundsException();
}
T old=theItems[index];
theItems[index]=val;
return old;
}
public boolean add(T val){
add(size(),val);
return true;
}
public void add(int index, T val) {
// TODO Auto-generated method stub
if(theItems.length==size()){
ensureCapacity(size()*+);
}
for(int i=theSize;i>index;i--){
theItems[i]=theItems[i-];
}
theItems[index]=val;
theSize++;
}
public T remove(int index){
T removedItem=theItems[index];
for(int i=index;i<size()-;i++){
theItems[i]=theItems[i+];
}
theSize--;
return removedItem;
}
public java.util.Iterator<T> iterator(){
return new ArrayListIterator();
}
private class ArrayListIterator implements java.util.Iterator<T>{
private int current=;
public boolean hasNext(){
return current<size();
}
public T next(){
if(!hasNext()){
throw new NoSuchElementException();
}
return theItems[current++];
}
public void remove(){
MyArrayList.this.remove(--current);
}
}
private void ensureCapacity(int newCapacity) {
// TODO Auto-generated method stub
if(newCapacity<theSize){
return;
}
T[] old=theItems;
theItems=(T[]) new Object[newCapacity];
for(int i=;i<size();i++){
theItems[i]=old[i];
}
}
private int size() {
// TODO Auto-generated method stub
return theSize;
} }
ArrayList数据结构的实现的更多相关文章
- 【集合框架】JDK1.8源码分析之ArrayList(六)
一.前言 分析了Map中主要的类之后,下面我们来分析Collection下面几种常见的类,如ArrayList.LinkedList.HashSet.TreeSet等.下面通过JDK源码来一起分析Ar ...
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...
- Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- ArrayList源代码深入剖析
第1部分 ArrayList介绍ArrayList底层采用数组实现,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, Cloneable, java. ...
- 【转】Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
原文网址:http://www.cnblogs.com/skywang12345/p/3308556.html 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具 ...
- Java 集合系列03之 ArrayList详细介绍
ArrayList做为List接口中最常用的实现类,必须掌握. 一.ArrayList简介 与Java中的数组相比ArrayList的容量可以动态增加.它继承与AbstractList,实现了List ...
- 精通ArrayList,关于ArrayList你想知道的一切
目录 精通ArrayList,关于ArrayList你想知道的一切 前言 ArrayList 内部结构,和常用方法实现 实例化方法 添加元素 add()方法 get()方法 移除元素 怎么扩容的 序列 ...
- java之ArrayList详细介绍
1 ArrayList介绍 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List ...
- ArrayList源码解读(jdk1.8)
概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...
随机推荐
- k-近邻算法-优化约会网站的配对效果
KNN原理 1. 假设有一个带有标签的样本数据集(训练样本集),其中包含每条数据与所属分类的对应关系. 2. 输入没有标签的新数据后,将新数据的每个特征与样本集中数据对应的特征进行比较. a. 计算新 ...
- gulp构建自动化项目
'use strict'; var gulp = require('gulp'), browserSync = require('browser-sync').create(), SSI = requ ...
- 四.idea本地调试hadoop程序
目录: 目录见文章1 1.先上案例代码 WordCount.java: import java.io.IOException; import java.util.StringTokenizer; im ...
- Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#<Object>‘的解决方法
发现问题 运行一下以前的一个Vue+webpack的 vue仿新闻网站 小项目,报错 由于自己vue学习不深入,老是这个报错,找了好久(确切的说是整整一下午^...^)才找到原因 -v- Uncau ...
- 深入理解javascript原型和闭包——从【自由变量】到【作用域链】
一直对闭包和变量作用域链模糊不清!!!有时都怀疑自己是不是脑袋秀逗啦还是范萌!! 先解释一下什么是“自由变量”. 在A作用域中使用的变量x,却没有在A作用域中声明(即在其他作用域中声明的),对于A作用 ...
- ExceptionLess的webAPI调用
引用 <package id="bootstrap" version="3.0.0" targetFramework="net461" ...
- BZOJ 2989: 数列/4170: 极光
题解: n倍经验题 首先比较容易想到的是对绝对值分类讨论 然后是4维偏序 1.查询和修改顺序 2.x>y 3.a[x]>a[y] 4.(x+a[x])-(y+a[y])<=k 这样是 ...
- Codeforces 653F Paper task SA
Paper task 如果不要求本质不同直接st表二分找出最右端, 然后计数就好了. 要求本质不同, 先求个sa, 然后用lcp求本质不同就好啦. #include<bits/stdc++.h& ...
- Codeforces 305E Playing with String 博弈
我们可以把每段连续可以选的字符看成一个游戏, 那么sg[ i ]表示连续 i 个字符可选的sg值. 然后找找第一个就好啦. #include<bits/stdc++.h> #define ...
- MySQL安装教程图解
核心提示:下面的是MySQL安装的图解,用的可执行文件安装的,详细说明了一下! 下面的是MySQL安装的图解,用的可执行文件安装的,详细说明了一下! MySQL下载地址 打开下载的mysql安装文件m ...