public Object getValue()
{
ValueItem valueItem = null;
Object returnValue = null;
if(this.value instanceof StringValueImpl)
{
valueItem = (StringValueImpl) this.value;
}
else if(this.value instanceof ListValueImpl)
{
valueItem = (ListValueImpl) this.value;
}
else if(this.value instanceof MapValueImpl)
{
valueItem = (MapValueImpl) this.value;
}
if(valueItem!=null)
returnValue = valueItem.getValue();
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
ValueItem是的interface这是由执行ListValueImpl,MapValueImpl等等.我想返回值是一个object.代码工作正常但我想知道这是否可以以任何方式改进?
public class Graph {
private Node node;
public void createGraph()
{
}
private class Node<K>{
K data;
List<Node> adjacent;
boolean visited;
Node()
{
adjacent = new ArrayList<Node>();
visited = false;
}
Node(K data)
{
this.data = data;
this.Node();
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器抱怨我不能调用this.Node()?
如果我有一个int[] array = {1, 2, 3}并且我想用下面的值初始化hashmap,有没有更好的方法呢?
Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
map.put(1,false);
map.put(2,false);
map.put(3,false);
Run Code Online (Sandbox Code Playgroud) final List<String> userIds = request.getUserIds();
final List<String> keys = userIds.stream().map(p -> {
return removePrefix(p);
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
基本上,列表中的每个键都userIds包含一个前缀"_user",我想为每个键删除它.所以,我removePrefix在列表的每个项目上调用该函数,并将该结果存储在另一个名为"keys"的列表中
antBuilder.copy(file: lstFile, todir:srcDir)
antBuilder.copy(file: lstGzippedFile, todir: srcDir)
antBuilder.copy(file: tarFile1, todir:srcDir)
antBuilder.copy(file: tarFile2, todir:srcDir)
antBuilder.copy(file: tarFile3, todir:srcDir)
Run Code Online (Sandbox Code Playgroud)
我可以通过组合文件参数和todir参数在1行中编写上面的代码
public boolean isPalindrome()
{
Stack myStack = new Stack();
for(Node current = head; current!=null; current = current.next)
{
if(!myStack.isEmpty())
{
if(myStack.peek()==current.data)
{
myStack.pop();
}else if(current.next!=null&&myStack.peek()==current.next.data)
{
continue;
}
else
{
myStack.push(current.data);
}
}else
{
myStack.push(current.data);
}
}
return myStack.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
我在这里做的是使用堆栈来检查链表是否是回文.它按预期工作只是我想摆脱代码重复,其他条件将数据推送到堆栈.
假设有一个数组声明:
int [] a = new int[3];
Run Code Online (Sandbox Code Playgroud)
我正在读一本书,说明我们需要在运行时显式创建数组的原因是因为我们无法知道在编译时为数组保留多少空间.那么,在上面的这种情况下,我们不知道我们需要保留底层java平台的int大小的3倍吗?
我正在编写一个正则表达式以匹配"日本"的每一次出现,并将其替换为"日本"..为什么以下不起作用?并且"日本"可以在句子中和句子中的任何地方多次出现.我想替换所有出现的事件
public static void testRegex()
{
String input = "The nonprofit civic organization shall comply with all other requirements of section 561.422, japan laws, in obtaining the temporary permits authorized by this act.";
String regex = "japan";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.out.println(input.matches(regex));
System.out.println(input.replaceAll(regex, "Japan"));
}
Run Code Online (Sandbox Code Playgroud) Should we OR exceptions like this ?
catch (final CustomExceptionA | CustomExceptionB e) {
Should we catch expections like this ?
}
Run Code Online (Sandbox Code Playgroud) 在文本文件中,我有以下内容:
${"a":"b"
}
${"a":"b"
}
${"a":"b"
}
${"a":"b"
}
Run Code Online (Sandbox Code Playgroud)
为什么我无法使用以下内容将字符串拆分为令牌数组?
String [] tokens = readFile().split("$");
Run Code Online (Sandbox Code Playgroud)
这里readFile()返回从文本文件中读取的整个字符串,该文件工作正常.我希望令牌数组将单个{"a":"b"}作为数组的四个元素返回.