Java-数组队列

1、为何要创建一个数组队列?

  数组的优点和缺点:

    优点:访问速度是所有数据结构中最快的一种。

    缺点:大小固定,如果要存储的数据个数不确定的时候?
           数组空间不够,导致越界异常发生
           如果空间太大了,数据不够,就会浪费内存空间
        插入、删除数据,的操作非常麻烦。 

  可见数组虽然有访问速度快的优点,但是数组的大小是固定了的,经常会出现空间不够或者数组越界的情况,并且删除和插入数据特别麻烦,因此就引入了数组队列的概念

2、数组队列的实现原理

   数组的大小是根据你要添加的数据来决定的。
   根据添加、删除数据的个数,来创建新的数组。
   数组名中存储的是数组对象在堆内存空间的首地址。
   新数组名中存储了新数组对象的首地址。
   把这个首地址给原来数组名。
   原来的数组对象,JVM(java虚拟机)的垃圾回收机制,会销毁对象,释放内存空间。

3、数组队列的具体实现方法

  数组队列:定义类,封装对数组的操作。
  数组队列的要求:
  在特殊情况,数组队列中只能存储某一种数据类型,如果存储其他的数据就报错。
  在特殊情况,数组中可以存储任意一种数据类型的数据
  上面两种情况的实现需要用到泛型<E>、<K、V>、<T>、...
     ①泛型不是Java中的一种数据类型。
     ②只是一个特殊的符号,可以在你不确定要存储什么类型的数据时,用这个符号代替Java中所有的数据类型。
   当你使用的时候,你可以用对应的数据类型来代替这个符号,这样
   就只能存储你指定的这一种数据类型;
   如果你不指定,则任意一种数据类型都可以存储。

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. }

结果显示:

Java-数组队列的更多相关文章

  1. java——数组队列 ArrayQueue

    队列: Array: package Date_pacage; public class Array<E> { //叫它静态数组 //private int[] data; private ...

  2. JAVA该队列中的数组,圆阵队列,链队列

    /** * 文件名:QueueText.java * 时间:2014年10月22下午9:05:13 * 笔者:维亚康姆维修 */ package chapter3; /** * 类名:ArrayQue ...

  3. Java中的自定义数组队列

    在Java中,作为所有数据结构中存储和获取速度最快的一种,数组凭借其这种简单易用的优势在各个方面都能大显神威.但是数组也有自身的局限性.数组的长度必须是固定的一旦定义之后就无法动态的更改,这就会造成这 ...

  4. JAVA之数组队列

    package xxj.datastructure0810; import java.util.Random; public class DataStructure { /** * @param ar ...

  5. 【栈和队列】5、队列概述与数组队列的基本实现 - Java

    3-5 数组队列 简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 队列Queue 队列也是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另 ...

  6. java数组实现队列

    数组队列 用数组实现的队列,也叫循环队列.就是定义一个数组,用两个下标head,tail表示队头和队尾.当队头和队尾相等时,队列为空.当队尾+1等于队头时,队列为满. 注意tail的值,当插入一个元素 ...

  7. 并发编程(八)—— Java 并发队列 BlockingQueue 实现之 ArrayBlockingQueue 源码分析

    开篇先介绍下 BlockingQueue 这个接口的规则,后面再看其实现. 阻塞队列概要 阻塞队列与我们平常接触的普通队列(LinkedList或ArrayList等)的最大不同点,在于阻塞队列的阻塞 ...

  8. java常用队列分析

    一.ArrayBlockingQueue 首先看一段源码: public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...

  9. 细说并发5:Java 阻塞队列源码分析(下)

    上一篇 细说并发4:Java 阻塞队列源码分析(上) 我们了解了 ArrayBlockingQueue, LinkedBlockingQueue 和 PriorityBlockingQueue,这篇文 ...

  10. 细说并发4:Java 阻塞队列源码分析(上)

    上篇文章 趣谈并发3:线程池的使用与执行流程 中我们了解到,线程池中需要使用阻塞队列来保存待执行的任务.这篇文章我们来详细了解下 Java 中的阻塞队列究竟是什么. 读完你将了解: 什么是阻塞队列 七 ...

随机推荐

  1. Difference between ReLU、LReLU、PReLU、CReLU、ELU、SELU

    激活函数 ReLU.LReLU.PReLU.CReLU.ELU.SELU  的定义和区别 ReLU tensorflow中:tf.nn.relu(features, name=None) LReLU ...

  2. php数组指针

    数组指针的操作: 移动数组指针的操作: Next() 向下 同时会获得当前元素的值. Prev() 向上同时会获得当前元素的值. End() 移动到最后一个元素单元 获得最后一个元素的值 Reset( ...

  3. AR中的SLAM(二)

    写在前面 本文想讨论一下AR的架构和SLAM在其中的作用. AR AR的框架可以简单划分为感知和交互两部分. 感知部分主要负责信息的收集和处理.信息主要通过不同的传感器收集,包括图像.设备加速度.距离 ...

  4. maven 骨架命令行创建

    项目的骨架maven 约定在项目的根目录下放置pom.xml,在src/main/java目录下放置主代码,在src/test/java下放置项目的测试代码. 这些基本的目录结构和pom.xml文件的 ...

  5. ASP.NET Claims-based认证实现认证登录-claims基础知识

    claims-based认证这种方式将认证和授权与登录代码分开,将认证和授权拆分成另外的web服务.活生生的例子就是我们的qq集成登录,未必qq集成登录采用的是claims-based认证这种模式,但 ...

  6. Docker 监控之 SaaS 解决方案

    过去的一年中,关于 Docker 的话题从未断过,而如今,从尝试 Docker 到最终决定使用 Docker 的转化率依然在逐步升高,关于 Docker 的讨论更是有增无减.另一方面,大家的注意力也渐 ...

  7. docker如何创建支持SSH服务的镜像

    一般情况下,Linux系统管理员通过SSH服务来管理操作系统,但Docker的很多镜像是不带SSH服务的,那么我们怎样才能管理操作系统呢?在第一部分中我们介绍了一些进入容器的办法,比如用attach. ...

  8. C# 数据上传(自用笔记)

    #region 数据上传 [HttpPost] public ActionResult UploadFile() { HttpFileCollectionBase files = Request.Fi ...

  9. MySQL: OPTIMIZE TABLE: Table does not support optimize, doing recreate + analyze instead

    show create table history;-------------------------- CREATE TABLE `foo` (  `itemid` bigint(20) unsig ...

  10. 转:spring 的控制反转

    文章一,原文地址:http://blog.sina.com.cn/s/blog_63804f6f0100kfx0.html 控制反转:       IoC(Inversion of Control,控 ...