复习数据结构,一个基本list
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyArrayList implements Iterable {
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private Object[] theItems;
public MyArrayList(){
clear();
}
public void clear() {
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size() {
return theSize;
}
public boolean isEmpty() {
return size() == 0;
}
public void trimToSize() {
ensureCapacity(size());
}
public Object get(int idx) {
if (idx < 0 || idx >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
return theItems[idx];
}
public Object set(int idx, Object newVal) {
if (idx < 0 || idx >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
Object old = theItems[idx];
theItems[idx] = newVal;
return old;
}
public void ensureCapacity(int newCapacity) {
if (newCapacity < theSize) {
return;
}
Object[] old = theItems;
theItems = (Object[]) new Object[newCapacity];
for (int i = 0; i < size(); i++) {
theItems[i] = old[i];
}
}
public boolean add(Object x) {
add(size(),x);
return true;
}
public void add(int idx,Object x) {
System.out.println("---"+theItems.length+"---"+size()+"--"+theSize+"--"+idx);
if (theItems.length == size()) {
ensureCapacity(size() * 2 + 1);
}
for(int i = theSize; i > idx;i--) {
theItems[i] = theItems[i-1];
System.out.println("===="+theItems[i-1]);
}
theItems[idx] = x;
theSize++;
}
public Object remove(int idx) {
Object removedItem = theItems[idx];
for (int i = idx; i < size() - 1 ;i++)
theItems[i] = theItems[i+1];
theSize--;
return removedItem;
}
@Override
public Iterator iterator() {
return new ArrayLIstIterator();
}
private class ArrayLIstIterator implements Iterator<Object>{
private int current = 0;
public boolean hasNext(){
return current < size();
}
public Object next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
return theItems[current++];
}
public void remove() {
MyArrayList.this.remove(--current);
}
}
}
分享到:
相关推荐
Java 实现泛型List的源码,基本实现了List接口的全部所有方法。欢迎大家发表自己的观点和建议。
用c++的STL模版list类库实现学生管理系统,已经实现了基本功能
通过c++是实现的List,实现了+和=的操作,就是添加和赋值的意思,也含有其它的基本方法,入push,pop等
2)数据结构里面两种非常重要的存储结构,线性结构中的连续存储结构(代表vector数组)和非连续存储结构(代表list链表),他们两者被广泛的应用在各个领域,是最基本最基础的两种存储结构; 3)之前的友元类只是...
对于c++编程爱好者而言,非常有用的。本文档包含链表实现技术已经常用的链表基本操作
选择合适的存储方式实现线性表。其中,必须实现的线性表基本操作为:InitList、 ClearList、ListEmpty、ListLength、GetElem、PriorElem、ListInsert、ListDelete这8个基本操作,其余的可以选作。
C语言实现顺序的建立、查找、插入、删除等基本操作
用C语言对单链表的一些基本操作的实现,简单易懂
其中,必须实现的线性表基本操作为:InitList、 ClearList、ListEmpty、ListLength、GetElem、PriorElem、ListInsert、ListDelete这8个基本操作,其余的可以选作。 2、 所写源代码编程风格良好,有详细注释。 3、 ...
C语言实现顺序表的基本操作,包括顺序表增删查操作以及顺序表的合并
系列博文的第一篇就从如何实现List开始。 需求 大家都知道基本链表得有以下特性:链表的初始化、链表的长度、节点的插入、删除、查找等一些常见的基本操作,最后写好之后,需要测试。关于测试,我之前写过Go的系列...
顺序存储结构线性表基本操作 纯C语言实现 a simple example of Sq_List by C language by wangweinoo1[PG]
树形控件节点的图标效果;list 控件添加多列数据及网格栏效果;listbox控件的添加项的基本使用; 有利于别人对这三个控件进行比较和学习; 很适合初学者对这三个控件的学习及掌握;
void CreateList_L(int n);//构造单链表 bool IsEmpty() const{return L->next == 0;}//判断单链表是否为空 int GetElem_L(int i,T &e) const;//当第i个元素存在时,其值赋给e int ListInsert_L(int i,T &e);//在第i...
模板链表类,实现链表基本操作:push、pop、front、back、qurey、insert、sort功能,抛出基本异常。
写的是一个链表,实现了添加和删除功能,遍历一遍链表,并对内容进行了排序
本案例是本人基于bootstrap和vue开发的一个to-do-list小案例,可以实现任务的删除,添加,任务列表的切换等基本功能。
自己基本测试了下可用,不过没有放入总条数之类的,长度有限,他的原理就是可以动态修改传递的start 以及 limit,如果有其他参数请在 store里面放入baseParams 参数如: , baseParams : { sort : 'foodprice ...
初始化一个顺序表,将顺序表所有元素置为0 SetList_Sq 建立顺序表,给表中各元素赋值SetList_Sq 销毁一个已存在的顺序表, 回收所分配的空间DestroyList_Sq 将已存在的顺序表重置为空表ClearList_Sq; 判断表是否为空...
list的基本实现技术 Python标准类型list就是一种元素个数可变的线性表,可以加入和删除元素,并在各种操作中维持已有元素的顺序(即保序),而且还具有以下行为特征: 基于下标(位置)的高效元素访问和更新,时间...