在Java编程中,我们学习了数组,但是在创建数组时必须给定数组的长度,这样数组的长度就不能改变了。在很多时候,我们可能要插入元素到数组中,这样我们没有办法实现,现在,学习了队列,就可以进行增、删、查、改等操作了。数组的优点就是:访问(存储)速度快
泛型:数组在不确定类型是用
使用格式:
public class List<E> {
private Object[] oldArray = new Object[0];
}
自定义数组分析:
假如定义了一个数组
String str=new String[5]{"a","b","c","d","e"};
那它的存储如下:
1、如果我们要在这个数组末尾插入一个h,怎么办呢?
我们可以再定义一个新数组数组长度比原先的数组长度长1
String newStr=new String[str.length+1];
然后在新数组末尾将h存入,再依次将原数组中的值copy到新数组,因为最终要得到的是原数组,所以我们再讲新数组的数组名赋给原数组的数组名,这样h就插入到原数组尾部了
str=newStr;
2、假如我们要在原数组的索引为2的后面插入一个h,怎么办?
和上面一样,只是原数组中的值copy到新数组中时是交叉copy的,如下
3、假如要删除索引为3的,怎么办?
一样的!!新数组长度为原数组长度-1
4、改变索引为2的元素的值为h,怎么办?
一样一样!!
好了,现在开始实现自定义队列:
package com.test.exercise;
public class List<E> {
private Object[] oldArray = new Object[0];
/*
* 在数组末尾添加一个元素
*/
public void add(E par) {
// 新建一个数组
Object[] newArray = new Object[oldArray.length + 1];
// 新数组末尾添加元素
newArray[oldArray.length] = par;
// 将原数组中的元素依次拷贝到新数组中
for (int i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
// 将新数组的数组名赋给原数组,即原数组存的地址是新数组存的地址
oldArray = newArray;
}
/*
* 在制定索引位置添加元素
*/
public void insert(E par, int index) {
// 新建一个数组
Object[] newArray = new Object[oldArray.length + 1];
// 新数组指定索引位置添加元素
newArray[index] = par;
// index前面的元素依次拷贝到新数组中
for (int i = 0; i < index; i++) {
newArray[i] = oldArray[i];
}
// index后的元素推后一位添加到新数组中
for (int i = index; i < oldArray.length; i++) {
newArray[i + 1] = oldArray[i];
}
// 将新数组的数组名赋给原数组,即原数组存的地址是新数组存的地址
oldArray = newArray;
}
/*
* 将指定索引删除
*/
public void delete(int index) {
// 新建一个数组
Object[] newArray = new Object[oldArray.length - 1];
// index前面的元素依次拷贝到新数组中
for (int i = 0; i < index; i++) {
newArray[i] = oldArray[i];
}
// index后的元素推前一位添加到新数组中
for (int i = index + 1; i < oldArray.length; i++) {
newArray[i - 1] = oldArray[i];
}
// 将新数组的数组名赋给原数组,即原数组存的地址是新数组存的地址
oldArray = newArray;
}
/*
* 获取指定索引位置的元素
*/
@SuppressWarnings("unchecked")
public E get(int index) {
return (E) oldArray[index];
}
/*
* 将指定索引位置的元素改变
*/
public void update(E par, int index) {
// 新建一个数组
Object[] newArray = new Object[oldArray.length];
// 指定索引位置的元素改为par
newArray[index] = par;
// 将原数组中索引index前的元素依次拷贝到新数组中
for (int i = 0; i < index; i++) {
newArray[i] = oldArray[i];
}
// 将原数组中索引index后的元素依次拷贝到新数组中
for (int i = index + 1; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
// 将新数组的数组名赋给原数组,即原数组存的地址是新数组存的地址
oldArray = newArray;
}
public int size() {
return oldArray.length;
}
/*
* index(Object o)和contains(Object bar)方法是参考源类写的
*/
/*
* 数组中有元素o,则返回o第一次出现的索引;若数组中没有元素o,则返回-1
*/
public int indexOf(Object o) {
// 如果o为空
if (o == null) {
// 遍历整个数组
for (int i = 0; i < oldArray.length; i++) {
/*
* 如果找到数组中某个索引位置为空,则返回i,当遍历整个数组都没有找到 则跳出循环执行return -1
*/
if (oldArray[i] == null) {
return i;
}
}// 如果o不为空
} else {
// 遍历整个数组
for (int i = 0; i < oldArray.length; i++) {
/*
* 如果找到数组中某个索引位置的值为o,则返回i,当遍历整个数组都没有找到 则跳出循环执行return -1
*/
if (o.equals(oldArray[i])) {
return i;
}
}
}
return -1;
}
/*
* 数组中有元素par,则返回true;否则返回false
*/
public boolean contains(Object par) {
return indexOf(par) >= 0;
}
}
package com.test.exercise;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
List<String> list = new List<String>();
list.add("12");
list.add("12");
list.add("14");
list.add("15");
list.delete(1);
System.out.println(list.indexOf("15"));
}
}