List模拟
来源:互联网 发布:身份证号码查询软件 编辑:程序博客网 时间:2024/05/17 21:59
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package arthur.datastruct.programe;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
*
* @author dell
*/
public class MyListText implements Iterable<Object> {
private static final int DEFAULT_CAPACITY = 10;
private int size;
private Object[] items;
/**
* clear the data in items
*/
public void clear() {
for (int i = 0; i < items.length; i++) {
items[i] = null;
}
}
/**
*return the size of MyList
* @return int
*/
public int size() {
return this.size;
}
/**
* to decide weather MyList is empty or not
* @return boolean
*/
public boolean isEmpty() {
return 0 == size;
}
/**
* get Object by id
* @param id
* @return
*/
public Object getObject(int id) {
if (id >= this.size() || id < 0) {
throw new ArrayIndexOutOfBoundsException();
}
return items[id];
}
public MyListText() {
items = new Object[DEFAULT_CAPACITY];
}
@Override
public Iterator<Object> iterator() {
return new ListIterator();
}
/**
* to update the data at number id in arraylist
* @param id the number in arrayList
* @param newObject the data you want to renew
* @return
*/
public Object set(int id, Object newObject) {
if (id >= this.size() || id < 0) {
throw new ArrayIndexOutOfBoundsException();
}
Object oldObject = this.items[id];
items[id] = newObject;
return oldObject;
}
/**
* add new Object to items,before add we must make the space is enough,
* if not ,double the items' space
* @param object
* @return
*/
public boolean add(Object object) {
//double the items' space
if (size == items.length) {
expandedLengthOfArray(items.length * 2);
}
items[size] = object;
size++;
return true;
}
/**
* add a new data for items,before executing adddition,we,before
* adding we must make the space is enough,
* if not ,double the items' space
* we should move
* back the data,and the size should be ++
* @param id
* @param object
*/
public void add(int id, Object object) {
//double the space if items has no enough space
if (items.length == this.size()) {
this.expandedLengthOfArray(size * 2);
}
//move back data
for (int i = this.size; i > id; i--) {
items[i] = items[i - 1];
}
items[id] = object;
this.size++;
}
/**
*
* to increase the length of array,
* if the newCapacity is less than the size,then
* this operation is not reasonable, else when executive this method, we must
* Copy the original data to make sure the data are not lost
* @param newCapacity
*/
public void expandedLengthOfArray(int newCapacity) {
// copy data
Object[] oldItems = this.items;
items = new Object[newCapacity];
System.arraycopy(oldItems, 0, items, 0, this.size);
}
/**
* remove the data at index position in items,we must make sure the data left
* afther remove move forward,and the total number of size will be --
* @param index
* @return
*/
public Object remove(int index) {
Object removeObject = items[index];
for (int i = index; i < this.size(); i++) {
items[i] = items[i + 1];
}
this.size--;
return removeObject;
}
private class ListIterator implements Iterator<Object> {
private int index;
@Override
public boolean hasNext() {
return this.index < size();
}
@Override
public Object next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
return items[index++];
}
@Override
public void remove() {
MyListText.this.remove(--index);
}
}
}
- List模拟
- poj 3125 list模拟
- <STL>模拟实现List
- C++::模拟实现List
- STL-模拟实现List
- 模拟实现 list
- list模拟实现
- 模拟实现list(迭代器)
- 【STL】模拟实现list
- 模拟实现list(iterator)
- 模拟实现list(迭代器)
- list简单模拟实现
- 模拟实现list
- 用js模拟的List
- STL List容器 简单模拟
- 【模拟】 hdu4409 Family Name List
- STL库list的模拟
- sublist分组list模拟代码
- linux系统中的信号
- C#和C++的主要区别
- 找工作的艰辛-寄给即将毕业找工作的人
- 正则表达式
- kalman滤波(转载)
- List模拟
- as400部署jboss
- 回调函数(c和指针)
- 广告上下移动
- SQL Server 全局变量
- 多核系统上的 Java 并发缺陷模式(bug patterns)
- 加速度计—幻想与现实
- Struts+jquery+ajax简单整合示例
- 解决JSP中文乱码问题