package jp.co.wap.exam;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
public class PersistentQueue
private List
public PersistentQueue () {
/ / modify this constructor if neccessary, but do not remove it
queue = new ArrayList
}
private PersistentQueue (List
/ / modify or remove this constructor if necessary
this.queue = queue;
}
/ / add other constructor if necessary
/ **
* Returns the queue that adds an item into the tail of this queue without modifying this queue
* @ param e
* @ return
* @ throws IllegalArgumentException
* /
public PersistentQueue
/ / TODO: make this method faster
if (e == null) {
throw new IllegalArgumentException ();
}
List
clone.add (e);
return new PersistentQueue
}
/ **
* Returns the queue that removes the object at the head of this queue without modifying this queue
*
* If this queue is empty, throws java.util.NoSuchElementException
* @ return
* @ throws
* / java.util.NoSuchElementException
* /
public PersistentQueue
/ / TODO: make this method faster
if (queue.isEmpty ()) {
throw new NoSuchElementException ();
}
List
clone.remove (0);
return new PersistentQueue
}
public E peek () {
/ / modify this method if needed
if (queue.isEmpty ()) {
throw new NoSuchElementException ();
}
return queue.get (0);
}
public int size () {
/ / modify this method if necessary
return queue.size ();
}
public static void main (String [] args) {
PersistentQueue
ps.enqueue ("string");
System.out.print (ps.peek ());
}
}
------ Solution ------------------------------------ --------
me how to write it so you can feel
public class PersistentQueue
------ Solution -------------------- ------------------------
where it feels out inefficient
------ Solution ------- -------------------------------------
queue using ArrayList to realize it is impossible to get up .
------ Solution ---------------------------------------- ----
in fact, this example does not need to insert and delete elements anywhere is a FIFO, the tail insert, delete the first team with a LinkedList of course also possible. This is a well-known IT company one should questions is to ask as much as possible to enhance the enqueue and dequeue speed.
Oh, as far as possible ah.
This is very pits.
Can you answer jni it ?
------ Solution ---------------------------------------- ----
queue why not LinkedList, this is the "Java programming ideas " Recommended .
specific principles are as follows:
LinkedList underlying implementation is a linked list ( doubly linked list ) , retrieval efficiency is low , insert, delete efficiency ;
ArrayList underlying implementation is an array , retrieval efficiency, insert, delete, and low efficiency.
queue is FIFO from an efficiency point of view , the most frequent operation should be inserted and removed , so use ArrayList LinkedList definitely better than high efficiency , as well as LinkedList landlord can look at the source code , the use of two-way circular list , achieve stack, queue -way , two-way queue can be met, but certainly not low efficiency , of course, you can own the principle according to which the algorithm to achieve their own .
------ Solution ---------------------------------------- ----
in fact, this example does not need to insert and delete elements anywhere is a FIFO, the tail insert, delete the first team with a LinkedList of course also possible. This is a well-known IT company one should questions is to ask as much as possible to enhance the enqueue and dequeue speed.
do recommend using LinkedList , LinkedList implements Deque interface is rather Deque is inherited from the Queue interface , so that you can use LinkedList as using the same queue , FIFO
------ For reference only ---------------------------------------
you think there is no further room for improvement ? I think there are still some . For example , when each into the team , because it is not changing the original contents of the queue , always want to re- new a List out, so do not drag on the speed
------ For reference only ------ ---------------------------------
Do you think there is no further room for improvement ? I think there are still some . For example , when each into the team , because it is not changing the original contents of the queue , always want to re- new a List out, so do not drag on the speed
No, just want to know what your starting point
everyone's starting point may be different , the general queue is used to enter the stack , the stack , empty , etc., etc. So you have a special use of this demand , the general is to construct a special queue for changing the original contents , for example, you can change the queue position of a specific element content , this demand is java.util.LinkedList
------ For reference only ---------------------------------- -----
I contacted java soon, I do not know in the JDK , which structures ArrayList faster than it can achieve FIFO , and look forward advise, thank you ! ! !
------ For reference only -------------------------------------- -
in fact, this example does not need to insert and delete elements anywhere is a FIFO, the tail insert, delete the first team with a LinkedList of course also possible. This is a well-known IT company one should questions is to ask as much as possible to enhance the enqueue and dequeue speed.
------ For reference only -------------------------------------- -
Oh, not estimated
------ For reference only ------------------------------ ---------
in fact, this example does not need to insert and delete elements anywhere is a FIFO, the tail insert, delete the first team with a LinkedList of course also possible. This is a well-known IT company one should questions is to ask as much as possible to enhance the enqueue and dequeue speed.
say that you can not change the beginning of the content is what do you mean ?
ArrayList based queue enqueue, dequeue slow movement is not due to other elements need to order ? Based on this consideration
------ For reference only ----------------------------------- ----
Thank you !
------ For reference only -------------------------------------- -
ah , thank you friends !
没有评论:
发表评论