Package myarraylist;
Import java.util.*;
/**
* Simulate the implementation of the ArrayList class in the JDK
* @author Iwang
*
*/
public class myArrayList {
/**
* The value is used for Object storage.
*/
Private object[] value;
/**
* The size is the number of Object used.
*/
private int size;
public void Rangecheck () {
try {
throw new Exception ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Public Object Set (int index,object obj) {
Rangecheck ();
Object old = Value[index];
Value[index] = obj;
return old;
}
public int indexOf (Object obj) {
if (obj==null) {
return-1;
}else{
for (int i=0;i<value.length;i++) {
if (Obj==value[i]) {
return i;
}
}
return-1;
}
}
public int lastIndexOf (Object obj) {
if (obj==null) {
return-1;
}else{
for (int i=value.length-1;i>=0;i--) {
if (Obj==value[i]) {
return i;
}
}
return-1;
}
}
public int size () {
return size;
}
public Boolean isEmpty () {
return size = = 0;
}
Public myArrayList () {
Value = new OBJECT[10];
This (10);
}
public myarraylist (int size) {
if (size<0) {
Rangecheck ();
}
Value = new Object[size];
}
public void InPut (Object obj) {
Value[size] = obj;
size++;
if (size>value.length-1) {
int capacity = (value.length << 1) + 2;
object[] NewList = new Object[capacity];
for (int i=0;i<value.length;i++) {
Newlist[i] = Value[i];
}
value = NewList;
}
}
Public Object getvalue (int index) {
if (index<0| | INDEX>SIZE-1) {
Rangecheck ();
}
return Value[index];
}
public static void Main (string[] args) {
myArrayList a = new myarraylist (2);
A.input ("ABCD");
A.input (New Human ("is ME");
A.input ("123456");
A.input ("123456");
A.input ("123456");
A.input ("123456");
A.input ("123456");
Human me = (Human) a.getvalue (1);
System.out.println (Me.getname ());
System.out.println (A.size ());
System.out.println (A.indexof ("ABCD"));
System.out.println (A.lastindexof ("ABCD"));
}
}
Package myarraylist;
public class Human {
private String name;
Public Human (String name) {
THIS.name = name;
}
Public String GetName () {
return this.name;
}
public void SetName (String name) {
THIS.name = name;
}
}
Java Learning Note-The underlying implementation of the simulation ArrayList container