本文整理汇总了Java中java.util.ArrayDeque.addLast方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayDeque.addLast方法的具体用法?Java ArrayDeque.addLast怎么用?Java ArrayDeque.addLast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.ArrayDeque
的用法示例。
在下文中一共展示了ArrayDeque.addLast方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startFromImpl
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* Generic implementation of BFS; forward/reverse edges are chosen using <code>selector</code>.
*
* @param g {@link DirectedGraph} to use for iteration.
* @param v Vertex to start from.
* @param visitor {@link VertexVisitor} to use for visiting vertices.
* @param selector {@link NeighborSelector} for selecting forward/reverse vertices.
*/
@Override
protected void startFromImpl(DirectedGraph<V, E> g, V v, VertexVisitor<V, E> visitor,
NeighborSelector<V, E> selector) {
final Set<V> seen = new HashSet<V>();
final ArrayDeque<V> dequeue = new ArrayDeque<V>();
dequeue.addLast(v);
while (!dequeue.isEmpty()) {
final V vertex = dequeue.pollLast();
if (!seen.contains(vertex)) { // skip seen ones
seen.add(vertex);
if (!visitor.visit(g, vertex)) {
break;
}
final Iterator<V> it = selector.nextFrom(g, vertex);
while (it.hasNext()) {
dequeue.add(it.next());
}
}
}
}
示例2: startFromImpl
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* Generic implementation of BFS; forward/reverse edges are chosen using <code>selector</code>.
*
* @param g {@link DirectedGraph} to use for iteration.
* @param v Vertex to start from.
* @param visitor {@link VertexVisitor} to use for visiting vertices.
* @param selector {@link NeighborSelector} for selecting forward/reverse vertices.
*/
@Override
protected void startFromImpl(DirectedGraph<V, E> g, V v, VertexVisitor<V, E> visitor,
NeighborSelector<V, E> selector) {
final Set<V> seen = new HashSet<V>();
final ArrayDeque<V> dequeue = new ArrayDeque<V>();
dequeue.addLast(v);
while (!dequeue.isEmpty()) {
final V vertex = dequeue.pollFirst();
if (!seen.contains(vertex)) { // skip seen ones
seen.add(vertex);
if (!visitor.visit(g, vertex)) {
break;
}
final Iterator<V> it = selector.nextFrom(g, vertex);
while (it.hasNext()) {
dequeue.add(it.next());
}
}
}
}
示例3: search
import java.util.ArrayDeque; //导入方法依赖的package包/类
public static int search(byte[] target, byte[] input) {
Object[] targetB = new Byte[target.length];
int x = 0;
while (x < target.length) {
targetB[x] = target[x];
++x;
}
int idx = -1;
ArrayDeque<Byte> q = new ArrayDeque<Byte>(input.length);
int i = 0;
while (i < input.length) {
if (q.size() == targetB.length) {
Object[] cur = q.toArray(new Byte[0]);
if (Arrays.equals(cur, targetB)) {
idx = i - targetB.length;
break;
}
q.pop();
q.addLast(input[i]);
} else {
q.addLast(input[i]);
}
++i;
}
return idx;
}
示例4: arrayDequeStuff
import java.util.ArrayDeque; //导入方法依赖的package包/类
static void arrayDequeStuff() {
ArrayDeque<Object> d = new ArrayDeque<>();
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.add(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offer(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.push(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.toArray(null);
// this should be fine
d.toArray();
}
示例5: main
import java.util.ArrayDeque; //导入方法依赖的package包/类
public static void main(String[] args) {
HashMap<Integer, Integer> map = new HashMap<>();
ArrayDeque<Integer> deque = new ArrayDeque<>();
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int max = 0;
for (int i = 0; i < n; i++) {
/* Remove old value (if necessary) */
if (i >= m) {
int old = deque.removeFirst();
if (map.get(old) == 1) {
map.remove(old);
} else {
map.merge(old, -1, Integer::sum);
}
}
/* Add new value */
int num = scan.nextInt();
deque.addLast(num);
map.merge(num, 1, Integer::sum);
max = Math.max(max, map.size());
}
scan.close();
System.out.println(max);
}
示例6: testToArray
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* toArray() and toArray(a) contain all elements in FIFO order
*/
public void testToArray() {
final int size = ThreadLocalRandom.current().nextInt(10);
ArrayDeque<Integer> q = new ArrayDeque<>(size);
for (int i = 0; i < size; i++) {
checkToArray(q);
q.addLast(i);
}
// Provoke wraparound
int added = size * 2;
for (int i = 0; i < added; i++) {
checkToArray(q);
assertEquals((Integer) i, q.poll());
q.addLast(size + i);
}
for (int i = 0; i < size; i++) {
checkToArray(q);
assertEquals((Integer) (added + i), q.poll());
}
}
示例7: main
import java.util.ArrayDeque; //导入方法依赖的package包/类
public static void main(String[] args) {
ArrayDeque<Integer> ad = new ArrayDeque<>();
// add element of array
ad.add(3);
ad.add(4);
System.out.println("Add Element. content: " + ad.toString());
// add element to its first (head) & last (tail) index
ad.addFirst(1);
ad.addLast(5);
System.out.println("AddFirst & AddLast. content: "+ ad.toString());
// offers are similar to adds
ad.offer(6);
ad.offer(7);
ad.offerFirst(0);
ad.offerLast(8);
System.out.println("Offers. content: " + ad.toString());
System.out.println("Current size: " + ad.size());
// peekFirst, peek, getFirst data
System.out.println("Peek First data is " + ad.peekFirst());
System.out.println("Peek data is " + ad.peek());
System.out.println("GetFirst is " + ad.getFirst());
// peekLast, getLast data
System.out.println("Peek Last data is " + ad.peekLast());
System.out.println("GetLast is " + ad.getLast());
// polls, get data & remove from queue
System.out.println("PollFirst data is " + ad.pollFirst() + ", content: " + ad.toString() );
System.out.println("Poll data is " + ad.poll()+ ", content: " + ad.toString());
System.out.println("PollLast data is " + ad.pollLast() + ", content: " + ad.toString());
}
示例8: startTrace
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* Build a new trace starting at {@code block}.
*
* @param debug
*/
@SuppressWarnings("try")
private Collection<AbstractBlockBase<?>> startTrace(DebugContext debug, AbstractBlockBase<?> block) {
ArrayDeque<AbstractBlockBase<?>> trace = new ArrayDeque<>();
try (Indent i = debug.logAndIndent("StartTrace: %s", block)) {
try (Indent indentFront = debug.logAndIndent("Head:")) {
for (AbstractBlockBase<?> currentBlock = block; currentBlock != null; currentBlock = selectPredecessor(currentBlock)) {
addBlockToTrace(debug, currentBlock);
trace.addFirst(currentBlock);
}
}
/* Number head blocks. Can not do this in the loop as we go backwards. */
int blockNr = 0;
for (AbstractBlockBase<?> b : trace) {
b.setLinearScanNumber(blockNr++);
}
try (Indent indentBack = debug.logAndIndent("Tail:")) {
for (AbstractBlockBase<?> currentBlock = selectSuccessor(block); currentBlock != null; currentBlock = selectSuccessor(currentBlock)) {
addBlockToTrace(debug, currentBlock);
trace.addLast(currentBlock);
/* This time we can number the blocks immediately as we go forwards. */
currentBlock.setLinearScanNumber(blockNr++);
}
}
}
debug.log("Trace: %s", trace);
return trace;
}
示例9: testAddLastNull
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* addLast(null) throws NPE
*/
public void testAddLastNull() {
ArrayDeque q = new ArrayDeque();
try {
q.addLast(null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例10: testAddLast
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* addLast(x) succeeds
*/
public void testAddLast() {
ArrayDeque q = new ArrayDeque();
q.addLast(zero);
q.addLast(one);
assertSame(zero, q.peekFirst());
assertSame(one, q.peekLast());
}
示例11: run
import java.util.ArrayDeque; //导入方法依赖的package包/类
void run() throws Throwable {
// System.out.printf(
// "iterations=%d size=%d, warmup=%1g, filter=\"%s\"%n",
// iterations, size, warmupSeconds, filter);
final ArrayList<Integer> al = new ArrayList<>(size);
// Populate collections with random data
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < size; i++)
al.add(rnd.nextInt(size));
final ArrayDeque<Integer> ad = new ArrayDeque<>(al);
final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size());
abq.addAll(al);
// shuffle circular array elements so they wrap
for (int i = 0, n = rnd.nextInt(size); i < n; i++) {
ad.addLast(ad.removeFirst());
abq.add(abq.remove());
}
ArrayList<Job> jobs = new ArrayList<>(Arrays.asList());
List.of(al, ad, abq,
new LinkedList<>(al),
new PriorityQueue<>(al),
new Vector<>(al),
new ConcurrentLinkedQueue<>(al),
new ConcurrentLinkedDeque<>(al),
new LinkedBlockingQueue<>(al),
new LinkedBlockingDeque<>(al),
new LinkedTransferQueue<>(al),
new PriorityBlockingQueue<>(al))
.stream()
.forEach(x -> {
jobs.addAll(collectionJobs(x));
if (x instanceof Deque)
jobs.addAll(dequeJobs((Deque<Integer>)x));
});
if (reverse) Collections.reverse(jobs);
if (shuffle) Collections.shuffle(jobs);
time(filter(filter, jobs));
}
示例12: populatedDeque
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* Returns a new deque of given size containing consecutive
* Integers 0 ... n - 1.
*/
private static ArrayDeque<Integer> populatedDeque(int n) {
// Randomize various aspects of memory layout, including
// capacity slop and wraparound.
final ArrayDeque<Integer> q;
ThreadLocalRandom rnd = ThreadLocalRandom.current();
switch (rnd.nextInt(6)) {
case 0: q = new ArrayDeque<Integer>(); break;
case 1: q = new ArrayDeque<Integer>(0); break;
case 2: q = new ArrayDeque<Integer>(1); break;
case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
case 4: q = new ArrayDeque<Integer>(n); break;
case 5: q = new ArrayDeque<Integer>(n + 1); break;
default: throw new AssertionError();
}
switch (rnd.nextInt(3)) {
case 0:
q.addFirst(42);
assertEquals((Integer) 42, q.removeLast());
break;
case 1:
q.addLast(42);
assertEquals((Integer) 42, q.removeFirst());
break;
case 2: /* do nothing */ break;
default: throw new AssertionError();
}
assertTrue(q.isEmpty());
if (rnd.nextBoolean())
for (int i = 0; i < n; i++)
assertTrue(q.offerLast((Integer) i));
else
for (int i = n; --i >= 0; )
q.addFirst((Integer) i);
assertEquals(n, q.size());
if (n > 0) {
assertFalse(q.isEmpty());
assertEquals((Integer) 0, q.peekFirst());
assertEquals((Integer) (n - 1), q.peekLast());
}
return q;
}