「java队列int」java队列实现
本篇文章给大家谈谈java队列int,以及java队列实现对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java中的队列都有哪些,有什么区别
阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞。试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其他的线程往空的队列插入新的元素。同样,试图往已满的阻塞队列中添加新元素的线程同样也会被阻塞,直到其他的线程使队列重新变得空闲起来,如从队列中移除一个或者多个元素,或者完全清空队列.
从5.0开始,JDK在java.util.concurrent包里提供了阻塞队列的官方实现。尽管JDK中已经包含了阻塞队列的官方实现,但是熟悉其背后的原理还是很有帮助的。一下是阻塞队列的实现:
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
java用数组实现队列
1.1. 队列的数据结构
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
1.2. Java实现
QueueTest
package ch04;
public class QueueTest {
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(10);
System.out.println(queue.isEmpty());
for (int i = 0; i 10; i++) {
queue.insert(i);
}
System.out.println(queue.isFull());
while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}
}
class ArrayQueue {
private int[] arrInt;// 内置数组
private int front;// 头指针
private int rear;// 尾指针
public ArrayQueue(int size) {
this.arrInt = new int[size];
front = 0;
rear = -1;
}
/**
* 判断队列是否为空
*
* @return
*/
public boolean isEmpty() {
return front == arrInt.length;
}
/**
* 判断队列是否已满
*
* @return
*/
public boolean isFull() {
return arrInt.length - 1 == rear;
}
/**
* 向队列的队尾插入一个元素
*/
public void insert(int item) {
if (isFull()) {
throw new RuntimeException("队列已满");
}
arrInt[++rear] = item;
}
/**
* 获得对头元素
*
* @return
*/
public int peekFront() {
return arrInt[front];
}
/**
* 获得队尾元素
*
* @return
*/
public int peekRear() {
return arrInt[rear];
}
/**
* 从队列的对头移除一个元素
*
* @return
*/
public int remove() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
return arrInt[front++];
}
}
运行结果如下:
false
true
1
2
3
4
5
6
7
8
9
用java编一个队列
自己写了个简单的实现
class QueueE{
private Object[] integerQueue;//用来当队列
public int tail;//队尾
public int size;//队的长度,也可以设置一个默认值,溢出时从新申请
public Queue(int size){
integerQueue=new Object[size];
this.size=size;
tail=-1;
}
/**
* 将元素插入队列
* @return 如果该元素已添加到此队列,则返回 true;否则返回 false
*/
public boolean offer(E e){
if(tail size-1){
tail++;
this.integerQueue[tail]=e;
return true;
}else{
return false;
}
}
/**
* 获取并移除此队列的头,如果此队列为空,则返回 null。
*/
public E poll(){
Object tmp;
if(tail=0){
tmp=this.integerQueue[tail];
tail--;
return (E)tmp;
}else{
return null;
}
}
}
java中怎么实现队列
public class QueueE {
private Object[] data=null;
private int maxSize; //队列容量
private int front; //队列头,允许删除
private int rear; //队列尾,允许插入
//构造函数
public Queue(){
this(10);
}
public Queue(int initialSize){
if(initialSize =0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
//判空
public boolean empty(){
return rear==front?true:false;
}
//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException("队列已满,无法插入新的元素!");
}else{
data[rear++]=e;
return true;
}
}
//返回队首元素,但不删除
public E peek(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
return (E) data[front];
}
}
//出队
public E poll(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
E value = (E) data[front]; //保留队列的front端的元素的值
data[front++] = null; //释放队列的front端的元素
return value;
}
}
//队列长度
public int length(){
return rear-front;
}
}
java队列int的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java队列实现、java队列int的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。