实现的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. HDU 5728 - PowMod

    HDU 5728 - PowMod 题意:    定义: k = ∑(i=1,m) φ(i∗n) mod 1000000007 给出: n,m,p ,且 n 无平方因子 求: ans= k^(k^(k ...

  2. CodeForces 501B - Misha and Changing Handles

    有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...

  3. A - 487-3279

    Description Businesses like to have memorable telephone numbers. One way to make a telephone number ...

  4. Dijkstra算法(迪杰斯塔拉算法)

    算法描述: Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最 ...

  5. QTestLib Tutorial

    本学习指南介绍了如何使用QTestLib框架的一些特性,分为4章: 编写一个单元测试程序 数据驱动测试 模拟GUI事件 重复GUI事件 第一章 编写一个单元测试程序 文件列表: qtestlib/tu ...

  6. PHP_CURL请求教程, 内含简单粗暴curl

    //curl访问 //需要url或者data //返回的数组是JSON数据形式 function ppd_curl($url,$data = null){ //\Think\Log::record($ ...

  7. Python成长之路第二篇(3)_字典的置函数用法

    字典的置函数用法(字典dict字典中的key不可以重复) class dict(object): """ dict() -> new empty dictionar ...

  8. 【测试环境】java|jdk|ant

    很多文章都有写啊,我只是汇总一下:现在java已经是1.7+了,但是我们很多的时候开发环境还是在1.5.16左右,需要自己去配置: 0.为了方便切换测试环境,我们可以把jdk放到一个比较固定的位置.比 ...

  9. SQL Server 找出值得优化的语句

    方法 1. sys.dm_exec_qurey_stats 返回 SQL Server 中缓存查询计划的聚合性能统计信息. 缓存计划中的每个查询语句在该视图中对应一行, 并且行的生存期与计划本身相关联 ...

  10. instsrv.exe下载和使用方法(微软Windows Server 2003 Resource Kit Tools工具中的一个)

    instsrv.exe是微软Windows Server 2003 Resource Kit Tools工具中的一个. Windows Server 2003 Resource Kit Tools是一 ...