package cn.pmcse.myCollection;
public class ArrayList {
private Object[] value;
private int count;//计数器 0
ArrayList(){
// value=new Object[16];
this(16);//this 必须首行
}
ArrayList(int objsize) {
value=new Object[objsize];//******************第二步 Object[] value=new Object[1]
}
public void copy(Object[] ar) {
if(value.length<ar.length) {
int newLength=ar.length*2;
Object[] t2=new Object[newLength];
value=t2;
}
for(int i=0;i<ar.length;i++) {
value[i]=ar[i];
}
}
public void add(Object obj) {
value[count]=obj;
System.out.println("********************"+count);//*************************** 0
count++;
System.out.println("********************"+count);//*************************** 1
if(count>=value.length) {
int newSize=value.length*2;//
Object[] newList=new Object[newSize];
for(int i=0;i<value.length;i++) {
newList[i]=value[i];
}
value=newList;
}
}
public Object get(int index) {
if(index<0||index>value.length-1) {//0-(length-1)
try {
throw new Exception();//手动抛出异常。
}catch(Exception e) {
e.printStackTrace();
}
}
return value[index];//返回value索引index的对象
}
public void set(int index,Object obj) {
if(index<0||index>value.length-1) {//0-(length-1)
try {
throw new Exception();//手动抛出异常。
}catch(Exception e) {
e.printStackTrace();
}
}
value[index]=obj;
}
public int size() {
return count;//返回数量
}
public int indexOf(Object obj) {
if(obj==null) {
return -1;
}else {
for(int i=0;i<value.length;i++) {
if(obj==value[i]) {
return 1;
}
}
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 1;
}
}
return -1;
}
}
public String toString() {
return "默认调用这个玩意儿toString()";
}
public static void main(String[] args) {
ArrayList list=new ArrayList(1); //******************第一步 size=1
list.add(new Human("阿莫西林"));
Human h=(Human)list.value[0];
h.setName("胶囊");
list.add("测试");
ArrayList list2=new ArrayList(1);
System.out.println(h.getName());//胶囊
System.out.println(list.get(1));//测试
System.out.println(list.count);//2
System.out.println(list.size());//2
System.out.println(list.value.length);//4
System.out.println(list.indexOf(h));
System.out.println(list.lastIndexOf(h));
System.out.println(list.value[3]);
list2.copy(list.value);
Human h2= (Human)list2.value[0];
list2.set(0, "对不对");
System.out.println(list2.value[0]);
System.out.println(list2.value.length);
System.out.println(list2.count);
System.out.println(h2.getName());
ArrayList ss =new ArrayList();
System.out.println(ss);
// for(int i=0;i<16;i++) {
// list.add(i);
// }
// System.out.println(list.value.length);
// System.out.println(list.value[31]);
}
}
word---------------------------------------------
import java.util.ArrayList;
public class MyArrayList {
private Object value[];
private int size;
public MyArrayList() {
// value = new Object[16];
this(2);
}
public MyArrayList(int size) {
value = new Object[size];
}
public void add(Object obj) {
value[size] = obj;
size++;
if (size >= value.length) {
// 装不下了,将要扩展容量了
int newCapacity = value.length * 2;
Object[] newList = new Object[newCapacity];
// System.arraycopy(src, srcPos, dest, destPos, length);
for (int i = 0; i < value.length; i++) {
newList[i] = value[i];
}
value = newList;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
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;// return 作用:1,返回出去值,2,结束语句。
}
}
public void rangleCheck(int index) {
if (index < 0 || index > size - 1) {// [0,size-1]
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Object set(int index, Object object) {
rangleCheck(index);
Object old = value[index];
value[index] = object;
return old;
}
public Object get(int index) {
rangleCheck(index);
return value[index];
}
public static void main(String[] args) {
MyArrayList list = new MyArrayList();
list.add("aaa");
list.add(new Human("高淇"));
list.add("bbb");
list.add("bbb");
list.add("bbb");
list.add("bbb");
Human h = (Human) list.get(1);
System.out.println(h.getName());
System.out.println("*********************");
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));
System.out.println("*******************");
System.out.println(list.size);
}
}
class Human {
private String name;
public Human(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}