Java-数组队列
Java-数组队列
1、为何要创建一个数组队列?
数组的优点和缺点:
优点:访问速度是所有数据结构中最快的一种。
缺点:大小固定,如果要存储的数据个数不确定的时候?
数组空间不够,导致越界异常发生
如果空间太大了,数据不够,就会浪费内存空间
插入、删除数据,的操作非常麻烦。
可见数组虽然有访问速度快的优点,但是数组的大小是固定了的,经常会出现空间不够或者数组越界的情况,并且删除和插入数据特别麻烦,因此就引入了数组队列的概念
2、数组队列的实现原理
数组的大小是根据你要添加的数据来决定的。
根据添加、删除数据的个数,来创建新的数组。
数组名中存储的是数组对象在堆内存空间的首地址。
新数组名中存储了新数组对象的首地址。
把这个首地址给原来数组名。
原来的数组对象,JVM(java虚拟机)的垃圾回收机制,会销毁对象,释放内存空间。
3、数组队列的具体实现方法
在特殊情况,数组队列中只能存储某一种数据类型,如果存储其他的数据就报错。
在特殊情况,数组中可以存储任意一种数据类型的数据
上面两种情况的实现需要用到泛型<E>、<K、V>、<T>、...
①泛型不是Java中的一种数据类型。
②只是一个特殊的符号,可以在你不确定要存储什么类型的数据时,用这个符号代替Java中所有的数据类型。
当你使用的时候,你可以用对应的数据类型来代替这个符号,这样
就只能存储你指定的这一种数据类型;
如果你不指定,则任意一种数据类型都可以存储。
4、自己定义一个数组队列的类
代码如下:
- package com.cyt.myarraylist0126;
- public class Myarraylist<e> {
- // 定义一个Object类型的数组
- private Object[] array = null;
- // 定义这个数组中已经添加的元素个数
- private int size = 0;
- // 通过构建函数初始化object数组
- public Myarraylist() {
- array = new Object[0];
- }
- public Myarraylist(int length) {
- array = new Object[length];
- }
- // 定义了一个往队列末尾添加元素的方法
- public void add(e stu) {
- if (array.length == 0 || array.length == size) {
- Object[] newarray = new Object[array.length + 1];
- for (int i = 0; i < array.length; i++) {
- newarray[i] = array[i];
- }
- newarray[array.length] = stu;
- array = newarray;
- size++;
- } else {
- array[size++] = stu;
- }
- }
- //定义了一个通过下标往队列数组里面插入元素的方法
- public boolean add(int index, e stu) {
- if (index < 0 || index >= array.length)
- return false;
- else {
- Object[] newarray = new Object[array.length + 1];
- for (int i = 0; i < array.length; i++) {
- newarray[i] = array[i];
- }
- array = newarray;
- for (int i = array.length - 1; i > index; i--) {
- array[i] = array[i - 1];
- }
- array[index] = stu;
- size++;
- return true;
- }
- }
- //定义了一个往队列数组通过下标添加队列数组的方法
- // C++的移动(需要经常复习)
- public boolean add(int index, Myarraylist<e> mal) {
- if (index < 0 || index >= array.length)
- return false;
- else if (array.length - size >= mal.size) {
- for (int i = size + mal.size - 1; i >= index + mal.size; i--) {
- array[i] = array[i - mal.size];
- }
- for (int i = index; i < index + mal.size; i++) {
- array[i] = mal.get(i - index);
- }
- size += mal.size;
- return true;
- } else {
- Object[] newarray = new Object[mal.size + size];
- for (int i = 0; i < size; i++) {
- newarray[i] = array[i];
- }
- array = newarray;
- for (int i = size + mal.size - 1; i >= index + mal.size; i--) {
- array[i] = array[i - mal.size];
- }
- for (int i = index; i < index + mal.size; i++) {
- array[i] = mal.get(i - index);
- }
- size += mal.size;
- return true;
- }
- }
- //定义了一个通过下标移除队列数组已有元素的方法
- public e remove(int index) {
- if (index < 0 || index >= size)
- return null;
- // 获取要移除的数据
- Object stu = array[index];
- int i;
- // 把index位置后的数据都往前移一位
- for (i = index + 1; i < size; i++)
- array[i - 1] = array[i];
- array[i - 1] = null;
- size--;
- return (e) stu;
- }
- //定义了一个移除队列元素中特定元素的方法
- public boolean remove(e stu) {
- int index = 0;
- for (index = 0; index < array.length; index++) {
- if (array[index] == stu) {
- break;
- }
- }
- if (index < 0 || index >= array.length)
- return false;
- else {
- // 获取要移除的数据
- Object stu2 = array[index];
- // 把index位置后的数据都往前移一位
- for (int i = index + 1; i < size; i++)
- array[i - 1] = array[i];
- size--;
- return true;
- }
- }
- //定义了一个移除队列中所有指定对象的方法
- // 非常易错的地方
- public int removeAll(e stu) {
- boolean a = true;
- while (a) {
- a = false;
- for (int i = 0; i < array.length; i++) {
- if (array[i] == stu) {
- a = true;
- int j;
- for (j = i; j < array.length - 1; j++) {
- array[j] = array[j + 1];
- }
- // 关键点
- array[j] = null;
- size--;
- break;
- }
- }
- }
- return 0;
- }
- //定义了一个通过下标更新元素的方法
- public boolean update(int index, e stu) {
- if (index < 0 || index >= array.length)
- return false;
- else {
- array[index] = stu;
- return true;
- }
- }
- //定义了一个更新所有指定对象的方法
- public int updateAll(e stu, e newstu) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == stu) {
- array[i] = newstu;
- }
- }
- return 0;
- }
- //定义了一个获取数组中特定下标元素的方法
- public e get(int index) {
- if (index < 0 || index >= size)
- return null;
- return (e) array[index];
- }
- //定义了一个获取队列数组中已有元素个数的方法
- public int size() {
- return size;
- }
- //定义了一个获取队列数组的方法
- public e[] getarray() {
- return (e[]) array;
- }
- }
5、测试自己创立的队列数组
代码如下
①首先创立了一个学生类
- public class Students {
- private String name;
- private int credit;
- private int age;
- public Students(String name,int credit,int age){
- this.name = name;
- this.credit = credit;
- this.age =age;
- }
- public void setName(String name){
- this.name = name;
- }
- public String getName(){
- return name;
- }
- public void setCredit(int credit){
- this.credit = credit;
- }
- public int getCredit(){
- return credit;
- }
- public void setaAge(int age){
- this.age = age;
- }
- public int getAge(){
- return age;
- }
- public void show(){
- System.out.println("Name "+ name+" Credit " + credit+ " Age "+ age+"\n");
- }
- }
②在主方法里面随机建立学生对象添加到队列数组里面
- public static void main(String[] args) {
- Myarraylist<Students> array = new Myarraylist();
- int size;
- String name = "";
- int credit;
- int age;
- Random random = new Random();
- // Students stu3 = new Students("CYT",5,18);
- // array.add(stu3);
- size = random.nextInt(5) + 1;
- for (int i = 0; i < size; i++) {
- credit = random.nextInt(5);
- age = random.nextInt(5) + 18;
- for (int j = 0; j < 4; j++) {
- name += (char) (random.nextInt(26) + 97);
- }
- array.add(new Students(name, credit, age));
- name = "";
- }
- // array.add(stu3);
- System.out.println("人数:" + size + "\n");
- System.out.println("人员信息:" + "\n");
- for (int i = 0; i < array.size(); i++) {
- Students stu = array.get(i);
- stu.show();
- }
结果显示:
Java-数组队列的更多相关文章
- java——数组队列 ArrayQueue
队列: Array: package Date_pacage; public class Array<E> { //叫它静态数组 //private int[] data; private ...
- JAVA该队列中的数组,圆阵队列,链队列
/** * 文件名:QueueText.java * 时间:2014年10月22下午9:05:13 * 笔者:维亚康姆维修 */ package chapter3; /** * 类名:ArrayQue ...
- Java中的自定义数组队列
在Java中,作为所有数据结构中存储和获取速度最快的一种,数组凭借其这种简单易用的优势在各个方面都能大显神威.但是数组也有自身的局限性.数组的长度必须是固定的一旦定义之后就无法动态的更改,这就会造成这 ...
- JAVA之数组队列
package xxj.datastructure0810; import java.util.Random; public class DataStructure { /** * @param ar ...
- 【栈和队列】5、队列概述与数组队列的基本实现 - Java
3-5 数组队列 简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 队列Queue 队列也是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另 ...
- java数组实现队列
数组队列 用数组实现的队列,也叫循环队列.就是定义一个数组,用两个下标head,tail表示队头和队尾.当队头和队尾相等时,队列为空.当队尾+1等于队头时,队列为满. 注意tail的值,当插入一个元素 ...
- 并发编程(八)—— Java 并发队列 BlockingQueue 实现之 ArrayBlockingQueue 源码分析
开篇先介绍下 BlockingQueue 这个接口的规则,后面再看其实现. 阻塞队列概要 阻塞队列与我们平常接触的普通队列(LinkedList或ArrayList等)的最大不同点,在于阻塞队列的阻塞 ...
- java常用队列分析
一.ArrayBlockingQueue 首先看一段源码: public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...
- 细说并发5:Java 阻塞队列源码分析(下)
上一篇 细说并发4:Java 阻塞队列源码分析(上) 我们了解了 ArrayBlockingQueue, LinkedBlockingQueue 和 PriorityBlockingQueue,这篇文 ...
- 细说并发4:Java 阻塞队列源码分析(上)
上篇文章 趣谈并发3:线程池的使用与执行流程 中我们了解到,线程池中需要使用阻塞队列来保存待执行的任务.这篇文章我们来详细了解下 Java 中的阻塞队列究竟是什么. 读完你将了解: 什么是阻塞队列 七 ...
随机推荐
- Difference between ReLU、LReLU、PReLU、CReLU、ELU、SELU
激活函数 ReLU.LReLU.PReLU.CReLU.ELU.SELU 的定义和区别 ReLU tensorflow中:tf.nn.relu(features, name=None) LReLU ...
- php数组指针
数组指针的操作: 移动数组指针的操作: Next() 向下 同时会获得当前元素的值. Prev() 向上同时会获得当前元素的值. End() 移动到最后一个元素单元 获得最后一个元素的值 Reset( ...
- AR中的SLAM(二)
写在前面 本文想讨论一下AR的架构和SLAM在其中的作用. AR AR的框架可以简单划分为感知和交互两部分. 感知部分主要负责信息的收集和处理.信息主要通过不同的传感器收集,包括图像.设备加速度.距离 ...
- maven 骨架命令行创建
项目的骨架maven 约定在项目的根目录下放置pom.xml,在src/main/java目录下放置主代码,在src/test/java下放置项目的测试代码. 这些基本的目录结构和pom.xml文件的 ...
- ASP.NET Claims-based认证实现认证登录-claims基础知识
claims-based认证这种方式将认证和授权与登录代码分开,将认证和授权拆分成另外的web服务.活生生的例子就是我们的qq集成登录,未必qq集成登录采用的是claims-based认证这种模式,但 ...
- Docker 监控之 SaaS 解决方案
过去的一年中,关于 Docker 的话题从未断过,而如今,从尝试 Docker 到最终决定使用 Docker 的转化率依然在逐步升高,关于 Docker 的讨论更是有增无减.另一方面,大家的注意力也渐 ...
- docker如何创建支持SSH服务的镜像
一般情况下,Linux系统管理员通过SSH服务来管理操作系统,但Docker的很多镜像是不带SSH服务的,那么我们怎样才能管理操作系统呢?在第一部分中我们介绍了一些进入容器的办法,比如用attach. ...
- C# 数据上传(自用笔记)
#region 数据上传 [HttpPost] public ActionResult UploadFile() { HttpFileCollectionBase files = Request.Fi ...
- MySQL: OPTIMIZE TABLE: Table does not support optimize, doing recreate + analyze instead
show create table history;-------------------------- CREATE TABLE `foo` ( `itemid` bigint(20) unsig ...
- 转:spring 的控制反转
文章一,原文地址:http://blog.sina.com.cn/s/blog_63804f6f0100kfx0.html 控制反转: IoC(Inversion of Control,控 ...