这是一种可接受的编码实践吗?
public class MessageFormat {
private static final Color DEFAULT_COLOR = Color.RED;
private Color messageColor = DEFAULT_COLOR;
public MessageFormat(Person person) {
Color color = person.getPreferredColor();
messageColor = (color != null) ? color : messageColor; // this line
}
}
Run Code Online (Sandbox Code Playgroud)
或者我最好还是选择经典......
if (color != null) {
messageColor = color;
}
Run Code Online (Sandbox Code Playgroud) 例如,用一条线一样previous,我希望模式匹配的线条p,pr,pre,prev,等等,所有的方式达到previous.我不希望它匹配像prevalent或的行previse.除了明显的(^(p|pr|pre|prev|...|previous)$)之外,是否有一种模式可以实现这一目标?
注意:我不需要像上面的模式那样捕获它,而且我使用Perl的正则表达式风格(在Perl中).
我正在尝试在C中实现一个链表,我想将头节点存储在一个单独的结构中.但是,每当我添加另一个节点时,似乎都会以某种方式重新分配头节点.
#include <stdio.h>
#include <stdlib.h>
struct BC_node {
struct BC_node *next;
void *data;
};
struct BC_list {
struct BC_node *head;
struct BC_node *tail;
};
void
BC_list_push(struct BC_list *list, void *data)
{
struct BC_node *node = calloc(1, sizeof(struct BC_node));
if (list->head != NULL)
printf("head: %d\n", *((int *) (list->head)->data));
node->next = NULL;
node->data = data;
if (list->head == NULL) {
printf("head is null.\n");
list->head = node;
}
if (list->tail != NULL) {
(list->tail)->next = node;
}
list->tail = node;
printf("head: %d\n", *((int …Run Code Online (Sandbox Code Playgroud)