if语句测试中使用的方法是否执行



免责声明:我是一名只有几个月经验的新程序员,如果这个问题微不足道,我深表歉意。

在Java中,if语句测试中的方法是否执行?例如,在这段代码中,我有一个类型为LinkedListQueue的等待列表变量,这是我为建模队列数据结构而编写的一个类,具有enqueue(((添加(、dequeue(删除(、size、setCapacity和isEmpty((等方法。dequeue((方法返回从队列中删除的对象。waitlist变量是int类型的LinkedQueue对象。我想遍历队列的每个元素,检查它是否等于3,如果等于,我会删除它。当if语句测试的代码行执行时,它会删除队列中的对象吗?

for (int i = 0; i < waitlist.size(); i++) {
if (waitlist.dequeue() == 3) {
waitlist.dequeue();
}
}

除非条件短路,否则if-statement条件内的方法调用将执行->您有一个OR,并且第一个片段已知为true,第二个片段未被检查,或者您有and,并且第一条片段已知为false,第二条片段未被检查。

if( true || test() ) // test isn't executed
if( false || test()) // test IS executed
if( true && test() ) // test IS executed
if( false && test() )// test isn't executed
if( test() ) // test IS executed
if( !test() )// test IS executed

这些值也可以是其他方法调用,比如:

if( alwaysTrue() || test() ) // test will never execute
if( alwaysFalse() || test() ) // test will ALWAYS execute
if( alwaysTrue() && test() ) // test will ALWAYS execute
if( alwaysFalse() && test() )// test will never execute

基本上,防止if-statement中的方法调用执行的唯一方法是在执行之前短路或引发异常。

如果你绝对需要在那里调用一个方法,那么考虑一个没有副作用的方法,比如只查看值,而不是从列表中删除它。

ArrayDeque实现和LinkedList实现似乎非常适合您的用例。

您要做的是取消排队并检查结果是否为3,如果是,则再次取消排队。如果你想使用它,你基本上是在跳过下一个

3号之后发生的一切,都将被移除;简单明了。

在使用尺寸时要小心,这样可能会随着物品的排队而改变。

public class Waitlist {
public static void main(String[] args) {
basicExample();     // Hello, World,
advancedExample();  // 2, 4, 6, 8,
exceptionExample(); // Success!
}
// Stops short, because one item was removed, this is unreliable.
private static void basicExample() {
Queue<String> waitlist = new LinkedListQueue<>();
waitlist.enequeue("Hello");
waitlist.enequeue("World");
System.out.printf("Before : %s%n", waitlist);
System.out.print("During : ");
for (int i = 0; i < waitlist.size(); i++) {
System.out.printf("%s... ", waitlist.dequeue());
}
System.out.printf("%nAfter  : %s%n%n", waitlist);
}
// Runs through the entire queue and removes, all items, printing even numbers.
private static void advancedExample() {
Queue<Integer> waitlist = new LinkedListQueue<>();
for (int i = 0; i < 10; i++) waitlist.enequeue(i);
System.out.printf("Before : %s%n", waitlist);
System.out.print("During : ");
while (!waitlist.isEmpty()) {
if (waitlist.dequeue() % 2 == 1 && !waitlist.isEmpty()) {
System.out.printf("%s... ", waitlist.dequeue());
}
}
System.out.printf("%nAfter  : %s%n%n", waitlist);
}
// Throws an exception
private static void exceptionExample() {
int max = 8;
Queue<Integer> waitlist = new LinkedListQueue<>(max); // max of 8
try {
for (int i = 0; i <= max; i++) {
waitlist.enequeue(i);
}
} catch (QueueFullException e) {
System.out.println("Success!"); // We reached capacity!
}
}
}

输出

Before : Hello, World
During : Hello... 
After  : World
Before : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
During : 2... 4... 6... 8... 
After  : 
Success!

LinkedListQueue.java

基于此,但我将其抽象化了。

import java.util.Iterator;
public class LinkedListQueue<T> implements Queue<T> {
private static class Node<E> {
private E data;
private Node<E> next;
public Node(E data) {
this.data = data;
next = null;
}
}
private Node<T> head, tail;
private int size, capacity;
public LinkedListQueue() {
this(Integer.MAX_VALUE);
}
public LinkedListQueue(int capacity) {
this.size = 0;
this.capacity = capacity;
}
@Override
public void setCapacity(int capacity) {
this.capacity = capacity;
}
@Override
public void enequeue(T data) throws QueueFullException {
if (this.size == this.capacity) {
throw new QueueFullException("The queue has reached its maximum capacity.");
}
Node<T> node = new Node<>(data);
if (head == null) {
head = node;
} else {
tail.next = node;
}
tail = node;
size++;
}
@Override
public T dequeue() {
if (isEmpty()) {
throw new NullPointerException("There is nothing to dequeue from this queue.");
}
T temp = head.data;
head = head.next;
size--;
return temp;
}
@Override
public boolean isEmpty() {
return head == null;
}
@Override
public int size() {
return size;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
Node<T> curr = head;
@Override
public boolean hasNext() {
return curr != null;
}
@Override
public T next() {
T data = curr.data;
curr = curr.next;
return data;
}
};
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Iterator<T> it = this.iterator();
while (it.hasNext()) {
builder.append(it.next() + ", ");
}
return builder.toString().replaceAll(", $", "");
}
}

队列.java

public interface Queue<T> extends Collection<T> {
void enequeue(T data) throws QueueFullException;
T dequeue();
void setCapacity(int capacity);
}

集合.java

public interface Collection<T> extends Iterable<T> {
boolean isEmpty();
int size();
}

QueueFullException.java

public class QueueFullException extends RuntimeException {
public QueueFullException() {
super();
}
public QueueFullException(String message) {
super(message);
}
}

最新更新