简易尝试自写ArrayList类

问题:
1.没有继承接口,因此是简易的容器,原型是ArrayList
2.只具备一些比较基本的方法

方法:
1.add
2.remove
3.get
4.set
5.toArray
6.toString

源码:

import java.util.Arrays;

public class Try_myList {
	private Object[] objects;
	private int size = 0;
	private int length = 0;
	public Try_myList() {
		size = 10;
		objects = new Object[size];
	}
	public Try_myList(int initsize) throws Exception {
		if(initsize <= 0) 
			throw new Exception("size can't less than 1");
		else {
			size = initsize;	
			objects = new Object[size];
		}
		
			
	}
	
	public void checkInitsize(int index) throws Exception {
		if(index >= size)
			throw new Exception("index out of range");	
	}
	
	public void add(int index,Object object) {
		try {
			checkInitsize(index);
			if(length == size-1) {
				size *= 3;
				Object[] myObjects = new Object[size];
				System.arraycopy(objects, 0, myObjects,0, index);
				myObjects[index] = object;
				System.arraycopy(objects,index,myObjects,index+1,objects.length-index);
				objects = myObjects;
			}
			else {
				System.arraycopy(objects,index,objects,index+1,length-index);
				objects[index] = object;
			}
			length++;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void add(Object object) {
		if(length == size-1) {
			size *= 3;
			Object[] myObjects = new Object[size];
			System.arraycopy(objects,0,myObjects,0,objects.length);
			myObjects[objects.length] = object;
			objects = myObjects;
		}
		else {
			objects[length] = object;
		}
		length++;
	}
	
	public void remove(Object object) {
		int index = 0;
		for(index = 0;index < objects.length;index ++) {
			if (objects[index].equals(object)) {
				System.arraycopy(objects,index+1,objects,index,objects.length-index-1);
				length--;
				break;
			}
		}
	}
	
	public void remove(int index) {
		try {
			checkInitsize(index);
			System.arraycopy(objects,index+1,objects,index,objects.length-index-1);
			length--;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Object get(int index) {
		try {
			checkInitsize(index);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return objects[index];
	}
	
	public Object set(int index,Object object) {
		try {
			checkInitsize(index);
			objects[index] = object;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return objects[index];
	
	}
	
	public boolean isEmpty() {
		return length == 0;
	}
	
	public int getSize() {
		return size;
	}

	public int getLength() {
		return length;
	}
	
	public Object[] toArray() {
		return Arrays.copyOf(objects, length);
	}
	
	@Override
	public String toString() {
		StringBuffer mBuffer = new StringBuffer();
		for (Object temp : objects) {
			mBuffer.append(temp.toString());
		}
		return mBuffer.toString();
	}

	public static void main(String[] args) {
		Try_myList testList = new Try_myList();
		System.out.println("length "+testList.getLength() +"  size: "+testList.getSize());
		testList.add("hello");
		testList.add("him");
		testList.add("he");
		testList.add("her");
		testList.add("she");
		testList.add(0,new Dog());
		testList.add(2,new Dog());
		testList.add(4,new Dog());
		testList.add(6,new Dog());
		testList.add(8,new Dog());
		System.out.println("length "+testList.getLength() +"  size: "+testList.getSize());
		System.out.println(testList.get(1));
		System.out.println(testList.get(3));
		testList.remove(9);
		System.out.println("length "+testList.getLength() +"  size: "+testList.getSize());
		for (Object temp : testList.toArray()) {
			System.out.println(temp.toString());
		}
		
		
	}
}


class Dog {
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值