package cn.bjsxt.array;
/**
* 根据StringBuilder类的相关源码创建属于自己的数组类
* 该类与jdk中的数组的区别在于该类中可以存储任何的对象
* jdk中的数组只能存储char
* @author 刘涛
*
*/
public class MyArrayList {
private int size;//数组的长度`
public MyArrayList(){
//无参构造器,默认初始化长度为16的数组
//value = new Object[16];
this(16);//此行代码和上面的代码含义相同,
}
public MyArrayList(int size){
}
//因为默认的size初始化值为0,所以从0开始,然后自增会继续往后加
value[size] = obj;
size++;
//对数组进行扩容,如果初始化数组的长度无法满足数组时便会进行自动的扩容操作
if(size >= value.length){
int newCapacity = size*2;
for(int i = 0; i < value.length;i++){
newList[i] = value[i];
}
value = newList;
}
}
if(index < 0 || index >= size){
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return value[index];
}
/**
* 删除数组中指定位置的元素,采取方式为数组中的最后一个元素替换将要删除的元素的
* @param index 要删除的元素的位置
*/
public void deleteCharAt(int index){
if (index < 0 || index > size) {
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
//将数组中的最后一个元素替换删除元素的即可
value[index] = value[size - 1];
//删除后数组的长度减1
size --;
//创建一个新的数组数组的长度为原来数组的长度减1,并将原来的数组中的元素复制到新数组中
for(int i = 0; i < size; i++){
newList[i] = value[i];
}
//System.arraycopy(value, 0, newList, 0, size);//将原始数组中的数据复制到新数组中的另一种方法
value = newList;
}
}
/**
* 删除数组中指定位置的元素,删除方式为删除指定位置的元素后,后面的元素向前移动
* @param index
*/
public void removeCharAt(int index){
//判断删除元素的位置是否在数组的长度范围内,若不在范围内则抛出异常
if (index < 0 || index > size) {
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
for(int i = 0; i < size; i ++){
value[i + index] = value[i + index + 1];
}
size --;
for(int i = 0; i < size; i ++){
newList[i] = value[i];
}
//System.arraycopy(value, 0, newList, 0, size);//将原始数组中的数据复制到新数组中的另一种方法
value = newList;
}
}
public void insertCharAt
(int index,
Object obj
){
if (index < 0 || index > size ) {
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
value[size] = obj;
size++;
System.
arraycopy(value,
0, newList,
0, size
);
value = newList;
}
}
/***
* 该方法还需要进行调整 目前还无法实现
* @param index
* @param obj
*/
public void insert
(int index,
Object obj
){
if (index < 0 || index > size ) {
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
size++;
for(int i = 0;i < size; i ++){
if((index + i + 1) > size){
value[index + i + 1] = null;
return;
}else{
value[index + 2 + i] = value[index + i + 1];
value[index] = obj;
}
}
System.
arraycopy(value,
0, newList,
0, size
);
value = newList;
}
}
/**
* 删除数组中指定区间的元素
* @param start 区间起始位置
* @param end 区间结束位置
*/
public void remove(int start,int end){
if(start < 0 || end > size || end < 0 || start > size){
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
for(int i = 0; i < size; i ++){
if(i+end > size){
value[i + start] = null;
}else{
value[i + start] = value[i + end];
}
}
size -= end - start;
for(int i = 0 ; i < size; i ++){
newList[i] = value[i];
}
//System.arraycopy(value, 0, newList, 0, size);//将原始数组中的数据复制到新数组中的另一种方法
value = newList;
}
}
public static void main
(String[] args
) {
MyArrayList my = new MyArrayList(2);
my.add("aaa");
my.add("bbb");
my.add("ccc");
my.add(new Human("孙悟空",500,true));
// Human h = (Human) my.get(3);
// System.out.println(h.getName());
// System.out.println(h.isGender());
// System.out.println(h.getAge());
// System.out.println(my.get(2));
// my.insertCharAt(3, "eee");
// my.insert(4, "fff");
// my.deleteChar(1);
// my.removeCharAt(1);
// my.remove(1, 3);
my.insert(3, "fff");
for(int i = 0; i < my.size; i ++){
System.
out.
println(my.
get(i
));
}
/*System.out.println(my.size);
Human h1 = (Human) my.get(1);
System.out.println(h1.getAge());*/
}
}