顺序表--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 { ...
随机推荐
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- Shell学习之Shift的用法
位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1 ...
- 【Android 错误记录】android.os.NetworkOnMainThreadException 异常问题
最近自己学习开发一个小app,想根据网络来判断一些逻辑,但是运行应用时遇到了这个错误 android.os.NetworkOnMainThreadException 后来,查询了一些信息,发现原因就是 ...
- WebService使用的一些总结
什么是WebService: 这个不用我在这里废话,网上的资料一搜一大把,如果你没有接触过这方面的知识,你可以先去网上查一下.这里我只想说一下我印象比较深刻的几点: WebService是基于soap ...
- 比较常见的const与指针的组合情况
1.对于普通的const与基本类型组合,都是表示的是这是一个常量, const int a; int const a; 表示的意思是一样的,a是一个常量,不可改变 2.对于const与指针组合在一起, ...
- C#多显示器转换的两种方法——SetWindowPos,Screen
原文 http://blog.csdn.net/hejialin666/article/details/6057551 实现多屏显示目的:一般情况下是一个电脑显示屏,外接一个电视显示屏.在电脑上显示的 ...
- tomcat优化-有改protocol 和 缓存 集群方案
tomcat优化 在线上环境中我们是采用了tomcat作为Web服务器,它的处理性能直接关系到用户体验,在平时的工作和学习中,归纳出以下七种调优经验. 1. 服务器资源 服务器所能提供CPU.内存.硬 ...
- poj1458 dp入门
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37551 Accepted: 15 ...
- python中pip的使用和安装
Ubuntu下安装pip的方法 安装pip的方法: Install pip and virtualenv for Ubuntu 10.10 Maverick and newer $ sudo ...
- javascript 事件处理
[写在前面]近期一直在看js的基础,毕竟jquery尽管好用,总归是用着别人写的,仅仅会用api不如搞清楚实现的原理. 等把js基础巩固好了一定要去读jquery的源代码. 事件流 事件流描写叙述的是 ...