Java-数组队列
1、为何要创建一个数组队列?
数组的优点和缺点:
优点:访问速度是所有数据结构中最快的一种。
缺点:大小固定,如果要存储的数据个数不确定的时候?
数组空间不够,导致越界异常发生
如果空间太大了,数据不够,就会浪费内存空间
插入、删除数据,的操作非常麻烦。
可见数组虽然有访问速度快的优点,但是数组的大小是固定了的,经常会出现空间不够或者数组越界的情况,并且删除和插入数据特别麻烦,因此就引入了数组队列的概念
2、数组队列的实现原理
数组的大小是根据你要添加的数据来决定的。
根据添加、删除数据的个数,来创建新的数组。
数组名中存储的是数组对象在堆内存空间的首地址。
新数组名中存储了新数组对象的首地址。
把这个首地址给原来数组名。
原来的数组对象,JVM(java虚拟机)的垃圾回收机制,会销毁对象,释放内存空间。
3、数组队列的具体实现方法
4、自己定义一个数组队列的类
代码如下:
1 package com.cyt.myarraylist0126; 2 3 public class Myarraylist<e> { 4 // 定义一个Object类型的数组 5 private Object[] array = null; 6 // 定义这个数组中已经添加的元素个数 7 private int size = 0; 8 9 // 通过构建函数初始化object数组 10 public Myarraylist() { 11 array = new Object[0]; 12 } 13 14 public Myarraylist(int length) { 15 array = new Object[length]; 16 } 17 18 // 定义了一个往队列末尾添加元素的方法 19 public void add(e stu) { 20 if (array.length == 0 || array.length == size) { 21 Object[] newarray = new Object[array.length + 1]; 22 for (int i = 0; i < array.length; i++) { 23 newarray[i] = array[i]; 24 } 25 newarray[array.length] = stu; 26 array = newarray; 27 size++; 28 } else { 29 array[size++] = stu; 30 } 31 } 32 //定义了一个通过下标往队列数组里面插入元素的方法 33 public boolean add(int index, e stu) { 34 if (index < 0 || index >= array.length) 35 return false; 36 else { 37 Object[] newarray = new Object[array.length + 1]; 38 for (int i = 0; i < array.length; i++) { 39 newarray[i] = array[i]; 40 } 41 array = newarray; 42 for (int i = array.length - 1; i > index; i--) { 43 array[i] = array[i - 1]; 44 } 45 array[index] = stu; 46 size++; 47 return true; 48 49 } 50 51 } 52 53 //定义了一个往队列数组通过下标添加队列数组的方法 54 // C++的移动(需要经常复习) 55 public boolean add(int index, Myarraylist<e> mal) { 56 if (index < 0 || index >= array.length) 57 return false; 58 else if (array.length - size >= mal.size) { 59 60 for (int i = size + mal.size - 1; i >= index + mal.size; i--) { 61 array[i] = array[i - mal.size]; 62 } 63 for (int i = index; i < index + mal.size; i++) { 64 array[i] = mal.get(i - index); 65 } 66 size += mal.size; 67 return true; 68 } else { 69 Object[] newarray = new Object[mal.size + size]; 70 for (int i = 0; i < size; i++) { 71 newarray[i] = array[i]; 72 } 73 array = newarray; 74 for (int i = size + mal.size - 1; i >= index + mal.size; i--) { 75 array[i] = array[i - mal.size]; 76 } 77 for (int i = index; i < index + mal.size; i++) { 78 array[i] = mal.get(i - index); 79 } 80 size += mal.size; 81 return true; 82 } 83 84 } 85 86 //定义了一个通过下标移除队列数组已有元素的方法 87 public e remove(int index) { 88 if (index < 0 || index >= size) 89 return null; 90 // 获取要移除的数据 91 Object stu = array[index]; 92 int i; 93 // 把index位置后的数据都往前移一位 94 for (i = index + 1; i < size; i++) 95 array[i - 1] = array[i]; 96 array[i - 1] = null; 97 size--; 98 return (e) stu; 99 }100 101 //定义了一个移除队列元素中特定元素的方法102 public boolean remove(e stu) {103 int index = 0;104 for (index = 0; index < array.length; index++) {105 if (array[index] == stu) {106 break;107 }108 }109 if (index < 0 || index >= array.length)110 return false;111 else {112 // 获取要移除的数据113 Object stu2 = array[index];114 // 把index位置后的数据都往前移一位115 for (int i = index + 1; i < size; i++)116 array[i - 1] = array[i];117 size--;118 return true;119 }120 }121 //定义了一个移除队列中所有指定对象的方法122 // 非常易错的地方123 public int removeAll(e stu) {124 boolean a = true;125 while (a) {126 a = false;127 for (int i = 0; i < array.length; i++) {128 if (array[i] == stu) {129 a = true;130 int j;131 for (j = i; j < array.length - 1; j++) {132 array[j] = array[j + 1];133 }134 // 关键点135 array[j] = null;136 size--;137 break;138 }139 }140 }141 142 return 0;143 }144 //定义了一个通过下标更新元素的方法145 public boolean update(int index, e stu) {146 if (index < 0 || index >= array.length)147 return false;148 else {149 array[index] = stu;150 return true;151 }152 }153 //定义了一个更新所有指定对象的方法154 public int updateAll(e stu, e newstu) {155 for (int i = 0; i < array.length; i++) {156 if (array[i] == stu) {157 array[i] = newstu;158 }159 }160 return 0;161 }162 //定义了一个获取数组中特定下标元素的方法163 public e get(int index) {164 if (index < 0 || index >= size)165 return null;166 return (e) array[index];167 }168 //定义了一个获取队列数组中已有元素个数的方法169 public int size() {170 return size;171 }172 //定义了一个获取队列数组的方法173 public e[] getarray() {174 return (e[]) array;175 }176 }
5、测试自己创立的队列数组
代码如下
①首先创立了一个学生类
1 public class Students { 2 private String name; 3 private int credit; 4 private int age; 5 public Students(String name,int credit,int age){ 6 this.name = name; 7 this.credit = credit; 8 this.age =age; 9 }10 11 public void setName(String name){12 this.name = name;13 }14 public String getName(){15 return name;16 }17 public void setCredit(int credit){18 this.credit = credit;19 }20 public int getCredit(){21 return credit;22 }23 public void setaAge(int age){24 this.age = age;25 }26 public int getAge(){27 return age;28 }29 public void show(){30 System.out.println("Name "+ name+" Credit " + credit+ " Age "+ age+"\n");31 }32 }
②在主方法里面随机建立学生对象添加到队列数组里面
1 public static void main(String[] args) { 2 Myarraylist<Students> array = new Myarraylist(); 3 int size; 4 String name = ""; 5 int credit; 6 int age; 7 Random random = new Random(); 8 // Students stu3 = new Students("CYT",5,18); 9 // array.add(stu3);10 size = random.nextInt(5) + 1;11 for (int i = 0; i < size; i++) {12 credit = random.nextInt(5);13 age = random.nextInt(5) + 18;14 for (int j = 0; j < 4; j++) {15 name += (char) (random.nextInt(26) + 97);16 }17 array.add(new Students(name, credit, age));18 name = "";19 }20 // array.add(stu3);21 22 System.out.println("人数:" + size + "\n");23 System.out.println("人员信息:" + "\n");24 for (int i = 0; i < array.size(); i++) {25 Students stu = array.get(i);26 stu.show();27 }
结果显示:
联系客服