实现的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. POJ 2446 Chessboard

    要求用占两格的长方形铺满平面上除去指定点 二分图匹配 #include <iostream> #include <cstdio> #include <cstring> ...

  2. linux 查看目录名称的方法

    1. ls -d * 2. grep查找以'/'结尾的,也就是目录 ls -F | grep '/$'

  3. 一种获取spring环境上下文方法:SpringContextUtil

    获得spring里注册Bean的有好几种方法,这里介绍一种比较简单的方法: import org.springframework.beans.BeansException; import org.sp ...

  4. 学习笔记 css3--选择器&新增颜色模式&文本相关

    Css3 选择器 --属性选择器 E[attr]只使用属性名,但没有确定任何属性值,E[attr="value"]指定属性名,并指定了该属性的属性值E[attr~="va ...

  5. Github获取仓库最新Release版本号API

    package me.chunsheng.hongbao.utils; import android.content.Context; import android.content.Intent; i ...

  6. NPOI导出多张图片到Excel

    常用NPOI导出数据到excel,但没有试过如何导出图片.NPOI最大的特点就是不依赖于Excel组件,服务端不需要安装Excel.在单元格中插入图片主要是用HSSFClientAnchor对象.他有 ...

  7. ubtuntu 下 使用svn命令

    SVN作为日常开发中不可缺少的工具,今天终于开始在Ubuntu下使用了. 1.首先需要安装SVN.Ubuntu下的SVN安装十分简单,sudo apt-get install subversion,然 ...

  8. Ubuntu 14.04卸载安装失败的Mysql数据库,以及重新安装配置

    一.删除原来Mysql 1.删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 2.删除mqsql的配置文件 sudo rm /etc/mysql/ -R 3.自动卸载my ...

  9. linux function

    #!/bin/bash function sayHello() { #这里的{ 和它前面的)之间可以没有空格 echo "Hello $1" } sayHello 'Neeky'

  10. YUI的模块化开发

    随着互联网应用越来越重,js代码越来越庞大,如何有效的去组织自己的代码,变得非常重要.我们应该学会去控制自己的代码,而不是到最后一堆bug完全不知道从哪冒出来.前端的模块化开发可以帮助我们有效的去管理 ...