java中的ArrayList的书写还是比较容易些的,所以就写了一遍
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayListsw {
private int capacity=2;
private Object [] a=new Object[capacity];
private int size=0;
/**
* 得到主存储数组a
* @return a数组
*/
public Object[] getA() {
return a;
}
/**
* 总元素的长度
* @return size
*/
public int getSize() {
return size;
}
public ArrayListsw(int capacity){
this.capacity=capacity;
a=new Object[capacity];
}
public ArrayListsw(){
}
/**
*
* @param ob
* 增加ArrayListsw里的元素,利用grow函数达到不会越界的效果
*/
public void add(Object ob){
if(size>=capacity){
this.grow();
}
a[size++]=ob;
}
/**
*
* @param index
* @param ob
*
* 增加ArrayListsw里的元素,利用grow函数达到不会越界的效果
*/
public void add(int index,Object ob){
if(size>=capacity){
this.grow();
}
Object [] b=new Object[capacity];
System.arraycopy(a, index, b, 0, a.length-index);
a[index]=ob;
System.arraycopy(b, 0, a, index+1, a.length-index-1);
size++;
}
/**
*
* @param ArrayListsw b
* 将b中所有的元素都放入this数组中
*/
public void addAll(ArrayListsw b){
while(size+b.getSize()>=capacity){
this.grow();
}
System.arraycopy(b.getA(), 0, a, size, b.getSize());
size+=b.getSize();
//System.out.println(toString(b.a,b.getSize()));
}
/**
* 从index开始的b中所有元素放入this的数组中
* @param index
* @param ArrayListsw b
*/
public void addAll(int index,ArrayListsw b){
while(size+b.getSize()-index>=capacity){
this.grow();
}
System.arraycopy(b.getA(), index, a, size, b.getSize());
size+=b.getSize()-index;
//System.out.println(toString(b.a,b.getSize()));
}
/**
* 将第index个元素删去
* @param index
*/
public void remove(int index){
if(index>=size){
throw new ArrayIndexOutOfBoundsException();
}
else{
size--;
Object [] b=new Object[capacity];
System.arraycopy(a, index+1, b, 0, a.length-index-1);
System.arraycopy(b, 0, a, index, a.length-index-1);
}
}
/**
* 将ArrayListsw中的ob元素删去
* @param ob
*/
public void remove(Object ob){
for(int i=0;i<size;i++){
if(a[i]==ob){
remove(i);
}
}
}
/**
* 将所有在b中的ArrayListsw删去
* @param ArrayListsw b
*/
public void removeAll(ArrayListsw b){
for(int i=0;i<size;i++){
for(int j=0;j<b.getSize();j++){
if(a[i]==b.getA()[j]){
remove(i);
}
}
}
}
/**
* 将index处插入一个Object
* @param index
* @param ob
* @return
*/
public Object set(int index,Object ob){
a[index]=ob;
return ob;
}
/**
* 获取index为下标的Object
* @param index
* @return
*/
public Object get(int index){
return a[index];
}
private void grow(){
int oldcapacity=capacity;
int newcapacity=oldcapacity+oldcapacity/2;
Object [] b=new Object[capacity];
b=a;
a=new Object[newcapacity];
System.arraycopy(b, 0, a, 0, b.length);
capacity=newcapacity;
}
/**
* 总元素的长度
* @return size
*/
public int size(){
return size;
}
/**
* 从start开始到end结束的所以元素打印出来
* @param start
* @param end
* @return
*/
public String subList(int start,int end){
Object [] b=new Object[capacity];
System.arraycopy(a, start, b, 0, end-start);
return toString(b,end-start);
}
/**
* 判断ob是否在a数组中
* @param ob
* @return
*/
public boolean contain(Object ob){
for(int i=0;i<size;i++){
if(ob==a[i])
return true;
}
return false;
}
/**
* 将第一个ob的下标返回
* @param ob
* 如果 存在
* @return 下标
*/
public int indexOf(Object ob){
for(int i=0;i<size;i++){
if(a[i]==ob){
return i;
}
}
return -1;
}
/**
* 从后往前,将第一个ob的下标返回
* @param ob
* 如果 存在
* @return 下标
*/
public int lastIndexOf(Object ob){
for(int i=size-1;i>=0;i--){
if(a[i]==ob){
return i;
}
}
return -1;
}
/**
* 判断是否为空
* @return
*/
public boolean isEpmty(){
if(size==0){
return true;
}
return false;
}
/**
* 迭代器
* @return
*/
public Iterator iterator() {
return new Itr();
}
private class Itr implements Iterator {
int cursor;
int lastRet = -1;
public boolean hasNext() {
return cursor != size;
}
public Object next() {
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = a;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return elementData[lastRet = i];
}
}
/**
* 初始化ArrayListsw里的元素
*/
public void clear(){
a=new Object[2];
size=0;
capacity=2;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(a);
result = prime * result + capacity;
result = prime * result + size;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ArrayListsw other = (ArrayListsw) obj;
if (!Arrays.equals(a, other.a))
return false;
if (capacity != other.capacity)
return false;
if (size != other.size)
return false;
return true;
}
@Override
public String toString() {
String s="[";
for(int i=0;i<size;i++){
if(i==size-1){
s+=a[i]+"]";
}
else{
s+=a[i]+", ";
}
}
return s;
}
public String toString(Object a[],int size) {
String s="[";
for(int i=0;i<size;i++){
if(i==size-1){
s+=a[i]+"]";
}
else{
s+=a[i]+", ";
}
}
return s;
}
}
测试:
import java.util.Iterator;
public class TestArrayList {
public static void main(String[] args) {
ArrayListsw list=new ArrayListsw();
ArrayListsw list2=new ArrayListsw();
list2.add(1);
list2.add(2);
list2.add("x");
list2.add('x');
list2.add(true);
list2.add(1.2);
list.add(1,5);
list.add(1);
list.add(2);
list.add(7);
list.add(1, "x");
list.add(2, "x");
System.out.println(list.contain("x"));
System.out.println(list.set(0,"yy"));
System.out.println(list.subList(0, 2));
System.out.println(list.get(0));
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println(list);
list.remove(1);
System.out.println(list);
System.out.println(list2);
list.addAll(list2);
System.out.println(list);
System.out.println(list.indexOf(5));
System.out.println(list.lastIndexOf("x"));
//list.removeAll(list2);
Iterator iter=list.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
}
}
结果:
测试的比较烂,随意看看