顺序表--MyArrayList的实现
实现的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的实现的更多相关文章
- C#顺序表 & 单向链表(无头)
C# 顺序表 非常标准的顺序表结构,等同于C#中的List<T>,但是List<T>在排错查询和数据结构替换上存在缺陷,一些情况会考虑使用自己定义的数据结构 1.优化方向 下表 ...
- 数据结构顺序表Java实现
Java实现顺序表算法:1:首先我们需要定义我们的接口,关于顺序表的一些基本的操作:顺序表中的操作都有增删改查. //List接口 public interface IList { //返回线性表的大 ...
- (java实现)顺序表-ArrayList
什么是顺序表 顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构. 在使用顺序表存储数据前,会先申请一段连续的内存空间(即数组),然后把数组依次存入 ...
- jdk顺序表笔记
一.AbstractCollection 提供了集合的最大实现 继承该类,必须实现size()和iterator(),因为该类操作集合都是通过iterator 二.fail-fast策略 该策略在集合 ...
- c++顺序表基本功能
头文件 #define LIST_MAX_SIZE 5#define LISTINCREMENT 2#include<assert.h>#include<string>temp ...
- 数据结构:顺序表(python版)
顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...
- 《数据结构》2.2顺序表(sequence list)
//顺序表节点的定义 typedef struct { datatype data[MAXSIZE]; //数组容量的上限 int len; //记录最后一个元素的位置,相当于一个指针,表空时len= ...
- c数据结构 顺序表和链表 相关操作
编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...
- java顺序表和树的实现
一.顺序表 1.线性表 //java顺序表的实现,如ArrayList就是用线性表实现的,优点是查找快,缺点是添加或删除要移动很多元素,速度慢 public class SequenceList { ...
随机推荐
- VS2010编译时出现错误1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
需要下载VS2010的补丁包
- 网易云数据结构- Maximum Subsequence Sum
题目 题目地址 思路 显然是最大子列和的进化版,那就先思考下经典的最大子列和.这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了. 存放答案的一个类,我把它看成一个袋子, ...
- Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TabLayout,TableLayout引起页面崩溃
在使用TableLayout的时候,运行引用程序直接Crash. FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 9703 java. ...
- 移动端-弹窗demo
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...
- wampserver 自定义站点
wampserver配置多站点,安装完wampserver后,我们要做的肯定是很多项目,那么如何配置wampserver多站点呢. 在“httpd.conf”文件中查找:Include conf/ex ...
- jquery settimeout使用
setTimeout(location,5000); //延迟5秒刷新页面 function location(){ window.location.href = window.location.hr ...
- MYSQL 查看表定义的 4 种方法
方法 1. show create table table_name;可以用这个看表的代码. show create table Strings; 方法 2. show full columns fr ...
- 如何设置 Internal 类,方法,属性对其他项目可见
internal 修饰符时程序集内部其他类可见.但对程序集外的类不可见,这样就很好的封装了可见性. 但是,我们也许会碰到类似的情况,特别是基于已有代码开发新的代码时,你会发现很多 internal 的 ...
- Activity Window View的关系
http://blog.csdn.net/chiuan/article/details/7062215 http://blog.163.com/fenglang_2006/blog/static/13 ...
- QTDesigner的QVBoxLayout自动随窗口拉伸
在MainWindow的构造函数中添加如下代码://设置Uiui.setupUi(this); //使Ui可自适应父窗口大小QVBoxLayout* mainLayout = new QVBoxLay ...