「java队列标题」java设置标题
本篇文章给大家谈谈java队列标题,以及java设置标题对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
怎么编写一个简单的java队列?
import java.util.*;
public class MyQueueT {
private LinkedListT list = new LinkedListT();
public void addLast(T v) {
list.addLast(v); //队尾插入
}
public T getFirst() {
return list.getFirst(); //取得队受元素
}
public void remove() {
list.removeFirst(); //移除队首元素
}
//类似功能自己扩展下
public static void main(String[] args) {
MyQueueString mq = new MyQueueString();
mq.addLast("hello world");
mq.addLast("hello world2");
System.out.println(mq.getFirst());
mq.remove();
System.out.println(mq.getFirst());
}
}
java中的队列都有哪些,有什么区别?
阻塞队列、普通队列,非阻塞队列。
阻塞队列与普通队列的而区别在于,当队列是空时,从队列中获取元素的操作会被阻塞,或则当队列是满的时,往队列中增加元素会被阻塞,试图从空的队列中取元素的线程或从满的队列中添加元素的线程同样会被阻塞。
队列的两个基本操作是inserting(插入)一个数据项,即把一个数据项放入队尾,另一个是removing(移除)一个数据项,即移除队头的数据项。这类似于电影爱好者排队买票时先排到队尾,然后到达队头买票后离开队列。
栈中的插入和移除数据项方法的命名是很标准,称为push和pop。队列的方法至今没有标准化的命名。“插入”可以称为put、add或enque,而“删除”可以叫delete、get或deque。插入数据项的队尾,也可以叫作back、tail或end。而移除数据项的队头,也可以叫head。下面将使用insert、remove、front和rear。
插入将值插入队尾,同时队尾箭头增加一,指向新的数据项。
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 队列
//通过LinkedList实现队列
package 队列和堆栈;
import java.util.*;
public class LinkedListQueueTest {
//字段
private LinkedList list;
//无参数构造
public LinkedListQueueTest()
{
list=new LinkedList();
}
//队列元素的个数
public int size()
{
return list.size();
}
//进入队列
public void enqueue(Object obj)
{
list.addLast(obj);
}
//对头出来
public Object dequeue()
{
return list.removeFirst();
}
//浏览对头元素
public Object front()
{
//return list.getFirst();
return list.peekFirst();
}
//判断队列是否为空
public boolean isEmpty()
{
return list.isEmpty();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedListQueueTest llq=new LinkedListQueueTest();
System.out.println(llq.isEmpty());
llq.enqueue("147");
llq.enqueue("258");
llq.enqueue("369");
System.out.println(llq.size());
System.out.println("移除队列头元素:"+llq.dequeue());
System.out.println(llq.size());
llq.enqueue("abc");
llq.enqueue("def");
System.out.println(llq.size());
System.out.println("查看队列的头元素:"+llq.front());
System.out.println(llq.size());
System.out.println(llq.isEmpty());
}
}
通过数组实现
package 队列和堆栈;
import java.util.NoSuchElementException;
//通过数组来实现队列
public class ArrayQueue {
//字段
public static Object[] data;
//队列的元素个数
protected int size ;
//队列头
protected int head;
//队列尾
public static int tail;
/**
*
*/
//无参数构造函数
public ArrayQueue() {
final int INITIAL_LENGTH=3;
data=new Object[INITIAL_LENGTH];
size=0;
head=0;
tail=-1;
}
//队列元素个数方法
public int size()
{
return size;
}
public boolean isEmpty()
{
return size==0;
}
//得到队列头元素
public Object front()
{
if(size==0)
throw new NoSuchElementException();
return data[head];
}
//进入队列enqueue()方法
public void enqueue(Object obj)
{
//此时队列已经满
if(size==data.length){
Object[] oldData=data;
data=new Object[data.length*2];
//if(head==0)
System.arraycopy(oldData, head, data, 0, oldData.length-head);
if(head0)
System.arraycopy(oldData, 0, data, head+1, tail+1);
head=0;
tail=oldData.length-1;
}
tail=(tail+1)%data.length;
size++;
data[tail]=obj;
}
//队列的元素出队
public Object dequeue()
{
if(size==0)
throw new NoSuchElementException();
Object ele=data[head];
//循环队列
head=(head+1)%data.length;
size--;
return ele;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
通过向量实现:
//通过向量实现栈
package 队列和堆栈;
import java.util.*;
public class VectorStackTest {
//字段
Vector v;
//构造函数
public VectorStackTest()
{
v=new Vector();
}
//元素的个数
public int size()
{
return v.size();
}
//是否为空
public boolean isEmpty()
{
return size()==0;
}
//进栈
public Object Push(Object obj)
{
v.addElement(obj);
return obj;
}
//出栈方法
public Object Pop()
{
int len=size();
Object obj=Peek();
v.removeElementAt(len-1);
return obj;
}
//查看栈顶元素
public Object Peek()
{
int len = size();
if (len == 0)
throw new EmptyStackException();
return v.elementAt(len - 1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
VectorStackTest vst=new VectorStackTest();
System.out.println("大小:"+vst.size());
vst.Push("123");
vst.Push("456");
vst.Push("789");
vst.Push("abc");
System.out.println("大小:"+vst.size());
System.out.println("栈顶:"+vst.Peek());
System.out.println("出栈:"+vst.Pop());
vst.Push("def");
vst.Push("147");
System.out.println("大小:"+vst.size());
System.out.println("栈顶:"+vst.Peek());
System.out.println("出栈:"+vst.Pop());
System.out.println(vst.Peek());
vst.Push("def");
vst.Push("147");
System.out.println(vst.Pop());
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println(vst.Pop());
System.out.println(vst.Pop());
vst.Push("1aadf");
vst.Push("2dafad");
vst.Push("123789");
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println("------------------end------------");
VectorStackTest llst=new VectorStackTest();
llst.Push("123");
llst.Push("456");
System.out.println("栈顶:"+llst.Peek());
System.out.println("出栈:"+llst.Pop());
System.out.println(llst.Peek());
llst.Push("789");
llst.Push("abc");
System.out.println("栈顶:"+llst.Peek());
System.out.println("出栈:"+llst.Pop());
System.out.println(llst.size());
System.out.println("栈顶:"+llst.Peek());
}
}
推荐:都看API文档。有疑问可以问我,QQ:285479197
Java队列问题
题目描述不是特别清晰
ListString list1; //size0
ListString list2 = new ArrayListString();
//如果不想改变原有队列,则new出新的Obj 并使他们的值一样
String a = list1.get(index); //index 为你需要的下标
String b=new String(a);
list2.add(b);
在java中,什么是队列?
java中没有队列这个东西吧...队列这个是出现在数据结构里面的吧..
然后 队列的结构是 先进先出..有队头对尾.数据从尾进...从头读出来
比如 1 2 3 4 5 这么一个队列....那么java读这个数据.是从1的队头开始读.....读到1 把1拿出来..然后队列剩下 2 3 4 5 ....如果要添加进去.那么就是 在对尾 就是5 的后面加一个 如3 ..那么就是 2 3 4 5 3 .这样的结构..
跟栈不同.栈是先进后出..这个你自己翻书吧..或者网上搜 数据结构 栈..就有了.
补充一下...java是有提供队列的类的.这个我就不说自己学会看api吧.
关于java队列标题和java设置标题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-14,除非注明,否则均为
原创文章,转载请注明出处。