实现的MyArrayList实为顺序表结构,其中要实现Iterable时必须在内部实现Iterator,即为该表的迭代器.

  public class MyArrayList<AntType> implements Iterable<AntType> {
@Override
public Iterator<AntType> iterator() { //实现接口
return new MyIterator();
}
private class MyIterator implements Iterator<AntType> { private int current = 0; @Override
public boolean hasNext() {
return current < size();
} @Override
public AntType next() {
if(!hasNext())
throw new NoSuchElementException();
return theItems[current ++];//迭代器从第一个元素开始迭代,即theItem[0]
}
public void remove(){
MyArrayList.this.remove(--current);
}
} private static final int DEFAULT_CAPACITY = 10; //设置默认容量 private int theSize; //当前大小
private AntType [] theItems; //元素对象数组 public MyArrayList(){ //构造函数,生成空表
clear();
} public void clear(){ //归为默认(清空)
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
} public void ensureCapacity(int newCapacity ){ //重置表的容量
AntType [] oldItems = theItems;
theItems = (AntType []) new Object[newCapacity];//****重新分配空间 注意使用强制转换的方式进行定义
for (int i = 0 ; i < theSize; i++){
theItems[i] = oldItems[i];
}
} public int size(){ //当前使用大小
return theSize;
} public boolean isEmpty(){ //判断是否为空
return theSize == 0;
} public void trimToSize(){ //将表的容量设为当前使用的大小
ensureCapacity(size());
} public AntType get(int idx){
//判断是否越界的合法性
if(idx < 0 || idx >= theSize)
throw new ArrayIndexOutOfBoundsException();//越界异常
else
return theItems[idx];
} //替换元素
public AntType set(int idx, AntType newVal){
if( idx < 0 || idx >= theSize)
throw new ArrayIndexOutOfBoundsException();
AntType oldVal = theItems[idx];
theItems[idx] = newVal;
return oldVal;
} public boolean add( AntType newVal){//末尾添加元素
if(theItems.length == size())
ensureCapacity(size() * 2 + 1);
set(theSize++ , newVal);
return true;
} public void add( int idx, AntType newVal){//任意位置添加元素
if(theItems.length == size())
ensureCapacity(size() * 2 + 1);
for(int i = size(); i > idx; i--)
theItems[i] = theItems[i - 1];
theItems[idx] = newVal;
theSize ++;
} public AntType remove(int idx) {
AntType val = theItems[idx];
for (int i = idx; i < theSize - 1; i++) {
theItems[i] = theItems[i + 1];
}
theSize--;
return val;
}
}

顺序表--MyArrayList的实现的更多相关文章

  1. C#顺序表 & 单向链表(无头)

    C# 顺序表 非常标准的顺序表结构,等同于C#中的List<T>,但是List<T>在排错查询和数据结构替换上存在缺陷,一些情况会考虑使用自己定义的数据结构 1.优化方向 下表 ...

  2. 数据结构顺序表Java实现

    Java实现顺序表算法:1:首先我们需要定义我们的接口,关于顺序表的一些基本的操作:顺序表中的操作都有增删改查. //List接口 public interface IList { //返回线性表的大 ...

  3. (java实现)顺序表-ArrayList

    什么是顺序表 顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构. 在使用顺序表存储数据前,会先申请一段连续的内存空间(即数组),然后把数组依次存入 ...

  4. jdk顺序表笔记

    一.AbstractCollection 提供了集合的最大实现 继承该类,必须实现size()和iterator(),因为该类操作集合都是通过iterator 二.fail-fast策略 该策略在集合 ...

  5. c++顺序表基本功能

    头文件 #define LIST_MAX_SIZE 5#define LISTINCREMENT 2#include<assert.h>#include<string>temp ...

  6. 数据结构:顺序表(python版)

    顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...

  7. 《数据结构》2.2顺序表(sequence list)

    //顺序表节点的定义 typedef struct { datatype data[MAXSIZE]; //数组容量的上限 int len; //记录最后一个元素的位置,相当于一个指针,表空时len= ...

  8. c数据结构 顺序表和链表 相关操作

    编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...

  9. java顺序表和树的实现

    一.顺序表 1.线性表 //java顺序表的实现,如ArrayList就是用线性表实现的,优点是查找快,缺点是添加或删除要移动很多元素,速度慢 public class SequenceList { ...

随机推荐

  1. html中的表格 bootstrap-table

    1 bootstrap-table 官网: http://bootstrap-table.wenzhixin.net.cn/ 2

  2. Java三大特征之多态(三)

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...

  3. webStorm 列编辑

    webStorm可以像Sublime一样使用列编辑,只是区别在于webStorm只可以编辑连续列表. 按住alt键鼠标选择一列,然后输入文字就会编辑多行,这个功能很赞,比较实用

  4. Data Mining 概念

    数据挖掘概念: 数据挖掘是在大型数据库中.自动的发现有用信息的过程. 然. 这个有用只是一个感性的东西.比如我们从表中索引一行数据.这个算不上数据挖掘.因为它依赖的是数据的明显特征. 数据挖掘基本步骤 ...

  5. Git学习01 --git add, git commit , git log ,git status, git reset --hard, head

    Git官方提供的快速入门教程:https://try.github.io/levels/1/challenges/1 特点:Git极其强大的分支管理:分布式版本 集中式版本控制系统,版本库是集中存放在 ...

  6. 阿里云CentOS 7.1使用yum安装MySql5.6.24

    正确的安装方法: 众所周知,Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本.所以我们需要先安装带有当前可用的m ...

  7. Qt 鼠标样式特效探索样例(一)——利用时间器调用QWidget.move()函数

    Qt 鼠标样式特效探索样例(一)       心血来潮,突然想在Qt里玩一把鼠标样式,想到在浏览网页时,经常看到漂亮的鼠标动画,于是今天摸索着乱写个粗糙的demo,来满足自己的好奇心. 效果图 方案要 ...

  8. C语言的本质(29)——C语言与汇编之寄存器和寻址方式

    x86的通用寄存器有eax.ebx.ecx.edx.edi.esi.这些寄存器在大多数指令中是可以任意选用的,比如movl指令可以把一个立即数传送到eax中,也可传送到ebx中.但也有一些指令规定只能 ...

  9. oracle参数文件spfile和pfile

    一.参数文件说明 PFILE(Initialiazation Parameter Files)初始化参数文件,是文本文件,可直使用文本编辑器查看.如果数据库使用的是初始化参数文件PFILE,要想永久修 ...

  10. 利用maven中resources插件的copy-resources目标进行资源copy和过滤

    maven用可以利用如下配置进行资源过滤,pom.xml的配置如下: <build> <!-- 主资源目录 --> <resources> <resource ...