java 数组实现_java根据数组源码写的自己的数组的简单的功能的实现

package cn.bjsxt.array;

/**

* 根据StringBuilder类的相关源码创建属于自己的数组类

* 该类与jdk中的数组的区别在于该类中可以存储任何的对象

* jdk中的数组只能存储char

* @author 刘涛

*

*/

public class MyArrayList {

private Object[] value;//定义数组

private int size;//数组的长度`

public MyArrayList(){

//无参构造器,默认初始化长度为16的数组

//value = new Object[16];

this(16);//此行代码和上面的代码含义相同,

}

public MyArrayList(int size){

value = new Object[size];

}

public void add(Object obj){

//因为默认的size初始化值为0,所以从0开始,然后自增会继续往后加

value[size] = obj;

size++;

//对数组进行扩容,如果初始化数组的长度无法满足数组时便会进行自动的扩容操作

if(size >= value.length){

int newCapacity = size*2;

Object[] newList = new Object[newCapacity];

for(int i = 0; i < value.length;i++){

newList[i] = value[i];

}

value = newList;

}

}

public Object get(int index){

if(index < 0 || index >= size){

try {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return value[index];

}

/**

* 删除数组中指定位置的元素,采取方式为数组中的最后一个元素替换将要删除的元素的

* @param index 要删除的元素的位置

*/

public void deleteCharAt(int index){

if (index < 0 || index > size) {

try {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

//将数组中的最后一个元素替换删除元素的即可

value[index] = value[size - 1];

//删除后数组的长度减1

size --;

//创建一个新的数组数组的长度为原来数组的长度减1,并将原来的数组中的元素复制到新数组中

Object[] newList = new Object[size];

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 {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

for(int i = 0; i < size; i ++){

value[i + index] = value[i + index + 1];

}

size --;

Object[] newList = new Object[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 {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

value[size] = obj;

size++;

Object[] newList = new Object[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 {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

size++;

for(int i = 0;i < size; i ++){

System.out.println(i);

if((index + i + 1) > size){

value[index + i + 1] = null;

return;

}else{

value[index + 2 + i] = value[index + i + 1];

value[index] = obj;

}

}

Object[] newList = new Object[size];

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 {

throw new Exception();

} catch (Exception e) {

// 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;

Object[] newList = new Object[size];

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));

System.out.println(my.size);

// my.insertCharAt(3, "eee");

// my.insert(4, "fff");

System.out.println();

// 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);

/*System.out.println(my.size);

Human h1 = (Human) my.get(1);

System.out.println(h1.getAge());*/

}

}package cn.bjsxt.array;

/**

* 根据StringBuilder类的相关源码创建属于自己的数组类

* 该类与jdk中的数组的区别在于该类中可以存储任何的对象

* jdk中的数组只能存储char

* @author 刘涛

*

*/

public class MyArrayList {

private Object[] value;//定义数组

private int size;//数组的长度`

public MyArrayList(){

//无参构造器,默认初始化长度为16的数组

//value = new Object[16];

this(16);//此行代码和上面的代码含义相同,

}

public MyArrayList(int size){

value = new Object[size];

}

public void add(Object obj){

//因为默认的size初始化值为0,所以从0开始,然后自增会继续往后加

value[size] = obj;

size++;

//对数组进行扩容,如果初始化数组的长度无法满足数组时便会进行自动的扩容操作

if(size >= value.length){

int newCapacity = size*2;

Object[] newList = new Object[newCapacity];

for(int i = 0; i < value.length;i++){

newList[i] = value[i];

}

value = newList;

}

}

public Object get(int index){

if(index < 0 || index >= size){

try {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return value[index];

}

/**

* 删除数组中指定位置的元素,采取方式为数组中的最后一个元素替换将要删除的元素的

* @param index 要删除的元素的位置

*/

public void deleteCharAt(int index){

if (index < 0 || index > size) {

try {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

//将数组中的最后一个元素替换删除元素的即可

value[index] = value[size - 1];

//删除后数组的长度减1

size --;

//创建一个新的数组数组的长度为原来数组的长度减1,并将原来的数组中的元素复制到新数组中

Object[] newList = new Object[size];

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 {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

for(int i = 0; i < size; i ++){

value[i + index] = value[i + index + 1];

}

size --;

Object[] newList = new Object[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 {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

value[size] = obj;

size++;

Object[] newList = new Object[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 {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else {

size++;

for(int i = 0;i < size; i ++){

System.out.println(i);

if((index + i + 1) > size){

value[index + i + 1] = null;

return;

}else{

value[index + 2 + i] = value[index + i + 1];

value[index] = obj;

}

}

Object[] newList = new Object[size];

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 {

throw new Exception();

} catch (Exception e) {

// 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;

Object[] newList = new Object[size];

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));

System.out.println(my.size);

//my.insertCharAt(3, "eee");

//my.insert(4, "fff");

System.out.println();

//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);

/*System.out.println(my.size);

Human h1 = (Human) my.get(1);

System.out.println(h1.getAge());*/

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值