珍爱生命, 远离JDK1.8 。。。
package 表;
import java.util.*;
@SuppressWarnings("unchecked")
public class MyArrayList <T>
{
private T[] arr = (T[])(new Object[10]);
public MyArrayList(){
super();
}
public MyArrayList(int defaultSize){
super();
ensureCap(defaultSize);
}
public MyArrayList(MyArrayList<T> model){
super();
add(model._getData());
}
public int size(){
int n = 0;
while(n < arr.length && arr[n] != null)
n++;
return n;
}
public int memorySize(){
return arr.length;
}
public boolean ensureCap(int sizeNeed){
if(sizeNeed <= arr.length)
return false;
T[] newArr = ((T[])new Object[sizeNeed]);
for(int i = 0; i < size(); i ++)
newArr[i] = arr[i];
arr = newArr;
return true;
}
public int add(int index, T element){
if(index < 0 || index > size()){
return -1;
}else{
if(arr.length == size()){
ensureCap(2*memorySize() + 1);
}
for(int i = size() - 1; i >= index; i--){
arr[i+1] = arr[i];
}
arr[index] = element;
}
return this.size();
}
public int add(T element){
return add(size(), element);
}
public int add(T[] tArr){
for(T s : tArr){
this.add(s);
}
return this.size();
}
public T get(int index){
if(index < 0 || index > size() - 1)
return null;
return arr[index];
}
public int remove(int index){
if(index < 0 || index >= size())
return -1;
for(int i = index + 1; i < size(); i ++){
arr[i-1] = arr[i];
}
arr[size() - 1] = null;
return size();
}
public void removeAll(){
int i = 0;
while(arr[i++] != null)
arr[i-1] = null;
return ;
}
public boolean contain(T obj){
for(int i = 0; i < size(); i ++){
if(arr[i].equals(obj))
return true;
}
return false;
}
public MyArrayList<T> union(MyArrayList<T> other){
MyArrayList<T> myArrayList = new MyArrayList<T>(other.size() + this.size());
for(int i = 0; i < size(); i ++)
myArrayList.add(this.get(i));
for(int j = 0; j < other.size(); j ++)
if(!this.contain(other.get(j)))
myArrayList.add(other.get(j));
return myArrayList;
}
public MyArrayList<T> intersect(MyArrayList<T> other){
MyArrayList<T> myArrayList = new MyArrayList<T>(other.size() > this.size() ? size() : other.size());
for(int i = 0; i < other.size(); i++){
if(this.contain(other.get(i))){
myArrayList.add(other.get(i));
}
}
return myArrayList;
}
public MyArrayList<T> merge(MyArrayList<T> another){
MyArrayList<T> myArrayList = new MyArrayList<T>(this);
myArrayList.add(another._getData());
return myArrayList;
}
@Override
public String toString()
{
String str = new String();
str += "data : ";
for(T t : arr)
if(t == null)
break;
else
str += t + " ";
return str;
}
public void sort(Comparator<T> comp){
for(int i = 0; i < size(); i ++){
for(int j = 0; j < size() - i - 1; j ++){
if(comp.compare(get(j), get(j+1)) > 0){
T t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
public T[] _getData(){
return this.arr;
}
}<span style="color:#ff0000;">
</span>
// TestMain
package 表;
import java.util.Comparator;
public class TestMain {
public static void main(String[] args)
{
// test cst.
MyArrayList<Integer> myArrayList0 = new MyArrayList<Integer>();
MyArrayList<Integer> myArrayList1 = new MyArrayList<Integer>(100);
MyArrayList<Integer> myArrayList2 = new MyArrayList<Integer>(myArrayList1);
System.out.println(myArrayList0.size());
System.out.println(myArrayList1.size());
System.out.println(myArrayList2.size());
System.out.println(myArrayList0.memorySize());
System.out.println(myArrayList1.memorySize());
System.out.println(myArrayList2.memorySize());
System.out.println("====================");
// test add(...) and remove(...);
System.out.println(myArrayList0.remove(3)); // failed
System.out.println(myArrayList0.add(4, new Integer(88))); // failed
System.out.println(myArrayList0.add(0, new Integer(1)));
System.out.println(myArrayList0.add(new Integer(2)));
System.out.println(myArrayList0.add(new Integer(3)));
System.out.println(myArrayList0.add(new Integer(4)));
System.out.println(myArrayList0.add(new Integer(5)));
System.out.println(myArrayList0.add(new Integer(6)));
System.out.println(myArrayList0.add(new Integer(7)));
System.out.println(myArrayList0.add(new Integer(8)));
System.out.println(myArrayList0.add(new Integer(9)));
System.out.println(myArrayList0.add(new Integer(10)));
System.out.println(myArrayList0.add(new Integer(11)));
System.out.println(myArrayList0.add(new Integer(12)));
System.out.println(myArrayList0.remove(5));
System.out.println(myArrayList0.add(new Integer(13)));
System.out.println(myArrayList0.add(new Integer(14)));
System.out.println("====================");
System.out.println(myArrayList0);
// test remove(...)
System.out.println(myArrayList0.remove(0));
System.out.println(myArrayList0);
// test merge
System.out.println(myArrayList1.add(new Integer(1)));
System.out.println(myArrayList1.add(new Integer(3)));
System.out.println(myArrayList1.add(new Integer(5)));
System.out.println(myArrayList1);
System.out.println(myArrayList0.union(myArrayList1));
System.out.println(myArrayList0.intersect(myArrayList1));
System.out.println(myArrayList0.merge(myArrayList1));
System.out.println("====================");
// test sort by rules
System.out.println("before sort " + myArrayList0);
myArrayList0.sort(new Comparator<Integer>(){
@Override
public int compare(Integer p1, Integer p2)
{
return (p1.intValue() > p2.intValue()) ? -1 : 1;
}
});
System.out.println("after sort " + myArrayList0);
// test clean up
myArrayList0.remove(0);
System.out.println(myArrayList0);
myArrayList0.removeAll();
System.out.println(myArrayList0);
System.out.println("End Test");
}
}
// output
0
0
0
10
100
10
====================
-1
-1
1
2
3
4
5
6
7
8
9
10
11
12
11
12
13
====================
data : 1 2 3 4 5 7 8 9 10 11 12 13 14
12
data : 2 3 4 5 7 8 9 10 11 12 13 14
1
2
3
data : 1 3 5
data : 2 3 4 5 7 8 9 10 11 12 13 14 1
data : 3 5
data : 2 3 4 5 7 8 9 10 11 12 13 14 1 3 5
====================
before sort data : 2 3 4 5 7 8 9 10 11 12 13 14
after sort data : 14 13 12 11 10 9 8 7 5 4 3 2
data : 13 12 11 10 9 8 7 5 4 3 2
data :
End Test