?
class="p0">数组队列
?
1.为什么要使用数组队列?
?
数组相当于是一个容器,可以存放多个相同类型的数据。
?
优点:有序性,清晰,可以快速地查找数据;具有连续的存储空间
?
缺点:在定义的时候数组长度已经固定,不可改变。例如在画板存储图形的时候,如果数组长度太小,会造成画了一定的图形之后,没有存储空间。如果数组长度太大,会造成存储空间的浪费。
?
2.数组队列的实现
?
数据类型?[?]?数组名?=?new?数据类型?[数组长度]
?
对象名中存储什么?对象在内存中的首地址。
?
数组名中存储什么?数组对象在内存中的首地址。
?
数组队列是通过改变数组的首地址来改变数组的长度和内容
?
3.数组队列实现步骤
?
1.定义一个接口,在接口中定义需要实现的抽象方法
?
2.定义一个类来实现接口,重写接口中的方法
?
3.实现方法
?
4.注意事项
?
1.首先要注意的是在实现抽象方法,比如移除、添加等方法的时候,注意数组队列长度的变化
?
2.一定要让原数组指向暂时存储数据的数组
?
?
//主函数
public class Manager {
public static void main(String[] args) {
System.out.println("开始测试");
MyList ml1 = new MyListImpl();
MyList ml2 = new MyListImpl();
for (int i = 0; i < 3; i++) {
ml2.add(new Student("m2学生" + i, i));
System.out.println("m2学生" + i + "学号" + i);
}
//
System.out.println("这是ml1");
for (int i = 0; i < 2; i++) {
ml1.add(new Student("学生" + i, i));
System.out.println("学生" + i + "学号" + i);
}
Student stu = new Student("学生1", 1);
// 添加新元素
ml1.add(stu);
System.out.println("给ml1加上stu");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 在指定位置添加一个新元素
ml1.add(1, stu);
System.out.println("给ml1在指定位置1添加一个新元素加上stu");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 将数组队列ml2中的数据添加到当前数组队列的末尾
ml1.add(ml2);
System.out.println("将数组队列ml2中的数据添加到当前数组队列的末尾");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 在指定位置2将参数数组队列ml2中的元素添加到该位置
ml1.add(2, ml2);
System.out.println("在指定位置2将参数数组队列ml2中的元素添加到该位置");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 移除指定索引位置的元素
ml1.remove(2);
System.out.println("移除指定索引位置2的元素");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 移除指定的元素对象
ml1.remove(stu);
System.out.println("移除指定的元素对象");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 移除mal数组队列中的元素
ml1.remove(ml2);
System.out.println("移除ml2数组队列中的元素");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 修改指定索引位置1的元素,用stu来代替
ml1.set(1, stu);
System.out.println("修改指定索引位置1的元素,用stu来代替");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 获取指定索引位置的元素
ml1.get(0);
System.out.println("获取指定索引位置的元素");
for (int i = 0; i < ml1.size(); i++) {
Student student = ml1.get(i);
System.out.println("学生是" + student.getName());
}
// 返回数组队列中首次出现的指定元素的索引
System.out.println("返回数组队列中首次出现的指定元素的索引" + ml1.indexOf(stu));
// 判断数组队列中是否有元素
System.out.println("判断数组队列中是否有元素" + ml1.isEmpty());
// 返回此列表中最后一次出现的指定元素的索引
System.out.println("返回此列表中最后一次出现的指定元素的索引" + ml1.lastIndexOf(stu));
// 清除数组队列中所有的元素
ml1.clear();
System.out.println("清除数组队列中所有的元素");
}
}
?
//接口
public interface MyList {
/**
* 添加新元素的方法
* @param stu是新元素
*/
public void add(Student stu);
/**
* 在指定位置添加一个新元素
* @param index指定的索引位置
* @param stu要添加的新元素
* @return 返回true表示添加成功,返回false表示添加失败
*/
public boolean add(int index ,Student stu);
/**
* 将数组队列mal中的数据添加到当前数组队列的末尾
* @param mal要被添加的数组队列
*/
public void add(MyList mal);
/**
* 在指定位置将参数数组队列中的元素添加到该位置
* @param index指定的索引位置
* @param mal要被添加的数组队列
* @return 返回true表示添加成功,返回false表示添加失败
*/
public boolean add(int index,MyList mal);
/**
* 移除指定索引位置的元素
* @param index指定的索引位置
* @return 返回被移除的对象,如果返回Null则表示index的索引不存在
*/
public Student remove(int index);
/**
* 移除指定的元素对象
* @param stu要被移除的元素
* @return 返回true表示移除成功,返回false表示移除失败
*/
public boolean remove(Student stu);
/**
* 移除mal数组队列中的元素
* @param mal要被移除的数组队列
* @return 返回true表示移除成功,返回false表示移除失败
*/
public boolean remove(MyList mal);
/**
* 修改指定索引位置的元素,用stu来代替
* @param index指定的索引位置
* @param stu要替换的新元素
* @return 返回被替换的对象,如果返回Null则表示index的索引不存在
*/
public Student set(int index,Student stu);
/**
* 获取指定索引位置的元素
* @param index指定的索引位置
* @return 返回索引位置的对象,如果返回Null则表示index的索引不存在
*/
public Student get(int index);
/**
* 返回数组队列中存储的元素总数
* @return 返回元素总数
*/
public int size();
/**
* 清除数组队列中所有的元素
*/
public void clear();
/**
* 返回数组队列中首次出现的指定元素的索引,或如果此数组队列不包含元素,则返回 -1。
* @param stu要查找的元素
* @return 返回元素的索引,如果返回-1表示元素不存在
*/
public int indexOf(Student stu);
/**
* 判断数组队列中是否有元素
* @return 返回true表示没有元素是空的;返回false表示非空
*/
public boolean isEmpty();
/**
* 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
* @param stu要查找的元素
* @return 返回元素的索引,如果返回-1表示元素不存在
*/
public int lastIndexOf(Student stu);
}
?
//实现接口的类
public class MyListImpl implements MyList {
private Student[] array = new Student[0];
public MyListImpl() {
}
/**
* 添加新元素的方法
*
* @param stu是新元素
*/
public void add(Student stu) {
Student[] temp = new Student[array.length + 1];
for (int i = 0; i < array.length; i++) {
temp[i] = array[i];
}
temp[array.length] = stu;
array = temp;
}
/**
* 在指定位置添加一个新元素
*
* @param index指定的索引位置
* @param stu要添加的新元素
* @return 返回true表示添加成功,返回false表示添加失败
*/
public boolean add(int index, Student stu) {
Student[] temp = new Student[array.length + 1];
if (index < array.length) {
for (int i = 0; i < index; i++) {
temp[i] = array[i];
}
temp[index] = stu;
for (int i = index; i < array.length; i++) {
temp[i + 1] = array[i];
}
array = temp;
return true;
} else {
return false;
}
}
/**
* 将数组队列mal中的数据添加到当前数组队列的末尾
*
* @param mal要被添加的数组队列
*/
public void add(MyList dest) {
// 是原长加上目标长
Student[] temp = new Student[array.length + dest.size()];
// copy src
for (int i = 0; i < array.length; i++) {
temp[i] = array[i];
}
for (int i = array.length; i < array.length + dest.size(); i++) {
temp[i] = dest.get(i - array.length);
}
array = temp;
}
/**
* 在指定位置将参数数组队列中的元素添加到该位置
*
* @param index指定的索引位置
* @param mal要被添加的数组队列
* @return 返回true表示添加成功,返回false表示添加失败
*/
public boolean add(int index, MyList mal) {
Student[] temp = new Student[array.length + mal.size()];
if (index < array.length) {
for (int i = 0; i < index; i++) {
temp[i] = array[i];
}
for (int i = index, j = 0; i < array.length + mal.size(); i++, j++) {
temp[i] = mal.get(j);
}
for (int i = index + mal.size(), j = index; i < array.length
+ mal.size(); j++, i++) {
temp[i] = array[j];
}
array = temp;
return true;
} else {
return false;
}
}
/**
* 移除指定索引位置的元素
*
* @param index指定的索引位置
* @return 返回被移除的对象,如果返回Null则表示index的索引不存在
*/
public Student remove(int index) {
Student[] temp = new Student[array.length - 1];
if (index < array.length) {
for (int i = 0; i < index; i++) {
temp[i] = array[i];
}
for (int i = index; i < array.length - 1; i++) {
temp[i] = array[i + 1];
}
Student indexdata = array[index];
array = temp;
return indexdata;
} else {
return null;
}
}
/**
* 移除指定的元素对象
*
* @param stu要被移除的元素
* @return 返回true表示移除成功,返回false表示移除失败
*/
public boolean remove(Student stu) {
int index = -1;
for (int j = 0; j < array.length; j++) {
if (array[j].equals(stu)) {
index = j;
Student[] temp = new Student[array.length - 1];
if (index < array.length) {
for (int i = 0; i < index; i++) {
temp[i] = array[i];
}
for (int i = index + 1; i < array.length; i++) {
temp[i - 1] = array[i];
}
array = temp;
}
}
}
if (index >= 0 && index < array.length) {
return true;
} else {
return false;
}
}
/**
* 移除mal数组队列中的元素
*
* @param mal要被移除的数组队列
* @return 返回true表示移除成功,返回false表示移除失败
*/
public boolean remove(MyList mal) {
Student[] temp = new Student[array.length - mal.size()];
int index = -1;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(mal.get(i))) {
index = i;
}
}
for (int j = 0; j < index; j++) {
temp[j] = array[j];
}
for (int j = index; j < array.length - mal.size(); j++) {
temp[j] = array[j + mal.size()];
}
array = temp;
if (index > 0 && index < array.length) {
return true;
} else {
return false;
}
}
/**
* 修改指定索引位置的元素,用stu来代替
*
* @param index指定的索引位置
* @param stu要替换的新元素
* @return 返回被替换的对象,如果返回Null则表示index的索引不存在
*/
public Student set(int index, Student stu) {
Student[] temp = new Student[array.length];
if (index < array.length) {
for (int i = 0; i < index; i++) {
temp[i] = array[i];
}
temp[index] = stu;
for (int i = index + 1; i < array.length; i++) {
temp[i] = array[i - 1];
}
array = temp;
return stu;
} else {
return null;
}
}
/**
* 获取指定索引位置的元素
*
* @param index指定的索引位置
* @return 返回索引位置的对象,如果返回Null则表示index的索引不存在
*/
public Student get(int index) {
if (index < array.length) {
return array[index];
} else {
return null;
}
}
/**
* 返回数组队列中存储的元素总数
*
* @return 返回元素总数
*/
public int size() {
return array.length;
}
/**
* 清除数组队列中所有的元素
*/
public void clear() {
for (int i = 0; i < array.length; i++) {
array[i] = null;
}
}
/**
* 返回数组队列中首次出现的指定元素的索引,或如果此数组队列不包含元素,则返回 -1。
*
* @param stu要查找的元素
* @return 返回元素的索引,如果返回-1表示元素不存在
*/
public int indexOf(Student stu) {
int index = -1;
for (int j = 0; j < array.length; j++) {
if (array[j].equals(stu)) {
index = j;
break;
}
}
return index;
}
/**
* 判断数组队列中是否有元素
*
* @return 返回true表示没有元素是空的;返回false表示非空
*/
public boolean isEmpty() {
if (array.length != 0) {
return true;
}
return false;
}
/**
* 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
*
* @param stu要查找的元素
* @return 返回元素的索引,如果返回-1表示元素不存在
*/
public int lastIndexOf(Student stu) {
int index = -1;
for (int j = array.length - 1; j >= 0; j--) {
if (array[j].equals(stu)) {
index = j;
break;
}
}
return index;
}
}
?
?
?
<!--EndFragment-->