本文整理汇总了Java中java.util.ArrayDeque.addFirst方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayDeque.addFirst方法的具体用法?Java ArrayDeque.addFirst怎么用?Java ArrayDeque.addFirst使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.ArrayDeque
的用法示例。
在下文中一共展示了ArrayDeque.addFirst方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: build
import java.util.ArrayDeque; //导入方法依赖的package包/类
static int build(int size, int B, ArrayDeque<Integer> chunks) {
chunks.addFirst(B);
if (size < BOUND1) {
chunks.addFirst(size);
return size;
}
int asize = (size + 2) / 2;
if (size >= BOUND2 && asize < BOUND1) {
asize = BOUND1;
} else if (size >= BOUND3 && asize < BOUND2) {
asize = BOUND2;
} else if (size >= BOUND4 && asize < BOUND3) {
asize = BOUND3;
} else if (size >= BOUND5 && asize < BOUND4) {
asize = BOUND4;
}
if (size - asize >= B) {
throw new AssertionError(" " + size + " , " + asize + " , " + B);
}
return build(asize, size - asize, chunks);
}
示例3: doIt
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* Your code goes here - see Part0 for an example
* @param r the reader to read from
* @param w the writer to write to
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
ArrayDeque<String> queue = new ArrayDeque<>();
for (String line = r.readLine(); line != null; line = r.readLine()) {
if (queue.size()>=42) {
if (line.length()==0) {
w.println(queue.getLast());
}
queue.removeLast();
}
queue.addFirst(line);
}
}
示例4: 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());
}
示例5: 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;
}
示例6: testAddFirstNull
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* addFirst(null) throws NPE
*/
public void testAddFirstNull() {
ArrayDeque q = new ArrayDeque();
try {
q.addFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例7: testAddFirst
import java.util.ArrayDeque; //导入方法依赖的package包/类
/**
* addFirst(x) succeeds
*/
public void testAddFirst() {
ArrayDeque q = new ArrayDeque();
q.addFirst(zero);
q.addFirst(one);
assertSame(one, q.peekFirst());
assertSame(zero, q.peekLast());
}
示例8: testNodesUpdatedAfterClusterStatePublished
import java.util.ArrayDeque; //导入方法依赖的package包/类
public void testNodesUpdatedAfterClusterStatePublished() throws Exception {
ThreadPool threadPool = new TestThreadPool(getClass().getName());
// randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
int minMasterNodes = randomBoolean() ? 3 : 1;
Settings settings = Settings.builder()
.put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
ArrayDeque<Closeable> toClose = new ArrayDeque<>();
try {
Set<DiscoveryNode> expectedFDNodes = null;
final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
masterTransport.start();
DiscoveryNode masterNode = masterTransport.getLocalNode();
toClose.addFirst(masterTransport);
ClusterState state = ClusterStateCreationUtils.state(masterNode, masterNode, masterNode);
// build the zen discovery and cluster service
ClusterService masterClusterService = createClusterService(threadPool, masterNode);
toClose.addFirst(masterClusterService);
// TODO: clustername shouldn't be stored twice in cluster service, but for now, work around it
state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
setState(masterClusterService, state);
ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
toClose.addFirst(masterZen);
masterTransport.acceptIncomingRequests();
final MockTransportService otherTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
otherTransport.start();
toClose.addFirst(otherTransport);
DiscoveryNode otherNode = otherTransport.getLocalNode();
final ClusterState otherState = ClusterState.builder(masterClusterService.getClusterName())
.nodes(DiscoveryNodes.builder().add(otherNode).localNodeId(otherNode.getId())).build();
ClusterService otherClusterService = createClusterService(threadPool, masterNode);
toClose.addFirst(otherClusterService);
setState(otherClusterService, otherState);
ZenDiscovery otherZen = buildZenDiscovery(settings, otherTransport, otherClusterService, threadPool);
toClose.addFirst(otherZen);
otherTransport.acceptIncomingRequests();
masterTransport.connectToNode(otherNode);
otherTransport.connectToNode(masterNode);
// a new cluster state with a new discovery node (we will test if the cluster state
// was updated by the presence of this node in NodesFaultDetection)
ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(
DiscoveryNodes.builder(state.nodes()).add(otherNode).masterNodeId(masterNode.getId())
).build();
try {
// publishing a new cluster state
ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
expectedFDNodes = masterZen.getFaultDetectionNodes();
masterZen.publish(clusterChangedEvent, listener);
listener.await(1, TimeUnit.HOURS);
// publish was a success, update expected FD nodes based on new cluster state
expectedFDNodes = fdNodesForState(newState, masterNode);
} catch (Discovery.FailedToCommitClusterStateException e) {
// not successful, so expectedFDNodes above should remain what it was originally assigned
assertEquals(3, minMasterNodes); // ensure min master nodes is the higher value, otherwise we shouldn't fail
}
assertEquals(expectedFDNodes, masterZen.getFaultDetectionNodes());
} finally {
IOUtils.close(toClose);
terminate(threadPool);
}
}
示例9: testPendingCSQueueIsClearedWhenClusterStatePublished
import java.util.ArrayDeque; //导入方法依赖的package包/类
public void testPendingCSQueueIsClearedWhenClusterStatePublished() throws Exception {
ThreadPool threadPool = new TestThreadPool(getClass().getName());
// randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
int minMasterNodes = randomBoolean() ? 3 : 1;
Settings settings = Settings.builder()
.put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
ArrayDeque<Closeable> toClose = new ArrayDeque<>();
try {
final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
masterTransport.start();
DiscoveryNode masterNode = masterTransport.getLocalNode();
toClose.addFirst(masterTransport);
ClusterState state = ClusterStateCreationUtils.state(masterNode, null, masterNode);
// build the zen discovery and cluster service
ClusterService masterClusterService = createClusterService(threadPool, masterNode);
toClose.addFirst(masterClusterService);
state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
setState(masterClusterService, state);
ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
toClose.addFirst(masterZen);
masterTransport.acceptIncomingRequests();
// inject a pending cluster state
masterZen.pendingClusterStatesQueue().addPending(ClusterState.builder(new ClusterName("foreign")).build());
// a new cluster state with a new discovery node (we will test if the cluster state
// was updated by the presence of this node in NodesFaultDetection)
ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(
DiscoveryNodes.builder(masterClusterService.state().nodes()).masterNodeId(masterNode.getId())
).build();
try {
// publishing a new cluster state
ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
masterZen.publish(clusterChangedEvent, listener);
listener.await(1, TimeUnit.HOURS);
// publish was a success, check that queue as cleared
assertThat(masterZen.pendingClusterStates(), emptyArray());
} catch (Discovery.FailedToCommitClusterStateException e) {
// not successful, so the pending queue should stay
assertThat(masterZen.pendingClusterStates(), arrayWithSize(1));
assertThat(masterZen.pendingClusterStates()[0].getClusterName().value(), equalTo("foreign"));
}
} finally {
IOUtils.close(toClose);
terminate(threadPool);
}
}
示例10: 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;
}