码迷,mamicode.com
首页 > 其他好文 > 详细

自己定义ArrayList 集合

时间:2015-09-06 21:32:09      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

 

package com.langsin.collection;
import java.util.List;
import org.junit.Test;
public class MyArrayList {
private Object[] elementData;
private int size;

public MyArrayList() {
// TODO Auto-generated constructor stub
this(10);
}
public MyArrayList(int initnum) {
if (initnum < 0) {
try {
throw new Exception();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
elementData = new Object[initnum];
}
private void add(Object obj) {
// TODO Auto-generated method stub
// 数组扩容和数据拷贝
if (size == elementData.length) {
Object[] newarray = new Object[size * 2 + 1];
System.arraycopy(elementData, 0, newarray, 0, elementData.length);
elementData = newarray;
}
elementData[size++] = obj;
}
private int size() {
return size;
// TODO Auto-generated method stub
}
private boolean isEmpty() {
return size == 0;
}
private Object get(int index) {
if (index < 0 || index >= size) {
try {
throw new Exception();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return elementData[index];
}
@Test
public void run() {
MyArrayList list = new MyArrayList(3);
list.add("qqq");
System.out.println(list.size());
}
}

代码编写时间:60%

JUnit测试时间:40%

 

自己定义ArrayList 集合

标签:

原文地址:http://www.cnblogs.com/charleslxk/p/4787122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!