假设我想要父节点匹配某些条件的所有节点.
除了检查每个节点并构建一个充满节点或子树的结果对象之外,是否有一种可接受的方法可以做到这一点?
我想做的事情如下:
Class<List<String>> targetClass = List<String>.class;
Run Code Online (Sandbox Code Playgroud)
但那个结构不能编译.
编译器可以正常使用
Class<List<String>> targetClass;
Run Code Online (Sandbox Code Playgroud)
声明,但编译器不喜欢
List<String>.class
Run Code Online (Sandbox Code Playgroud)
有趣的是,编译器(1.7)允许这样:
Class<List<String>> targetClass = (Class<List<String>>) List.class;
Run Code Online (Sandbox Code Playgroud)
但当然抱怨一个不安全的演员阵容.
给定一个使用spring的测试实用程序注释ContextConfiguration来创建bean的testng类,bean只在测试类的生命周期中创建一次.
在使用它之前,我总是使用@BeforeMethod在每个@Test方法之前重建所有内容.
我的问题:有没有办法让spring为每个@Test方法重建bean?
//The beans are unfortunately created only once for the life of the class.
@ContextConfiguration( locations = { "/path/to/my/test/beans.xml"})
public class Foo {
@BeforeMethod
public void setUp() throws Exception {
//I am run for every test in the class
}
@AfterMethod
public void tearDown() throws Exception {
//I am run for every test in the class
}
@Test
public void testNiceTest1() throws Exception { }
@Test
public void testNiceTest2() throws Exception { }
}
Run Code Online (Sandbox Code Playgroud) 我问的是关于成员"mylist"的更好的做法是什么:
像这样声明mylist:
class MyClaz {
std::list<string> mylist;
...
}
Run Code Online (Sandbox Code Playgroud)
或者像这样声明:
class MyClaz {
std::list<string> *mylst;
MyClaz();
...
}
MyClaz::MyClaz() {
myList = new std::list<string>();
}
// plus destructor to delete myList
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,当创建MyClaz实例时,是否会自动分配mylist?当MyClaz被销毁时,它会被正确清理吗?
是否有理由使用第二种情况?这似乎更危险.
我想出了一个只能在每10,20,30 ...... 100,200,300 ......,1000,2000中触发的一个,但我想知道是否有更好的方法来做到这一点.
unsigned long fails = 0;
while (true) {
if (!checkSomething()) {
fails++;
unsigned long backoff = exp10(((unsigned long) log10(fails)));
if (fails % backoff == 0)
logError("...");
}
}
Run Code Online (Sandbox Code Playgroud) 无法弄清楚这一点.
这在machinex.com上本地工作:
hg clone /somepath/repos mystuff
Run Code Online (Sandbox Code Playgroud)
但我尝试远程:
hg clone ssh://myuser@machinex.com/somepath/repos mystuff
Run Code Online (Sandbox Code Playgroud)
我明白了:
myuser's password: [enter password correctly]
remote: abort: There is no Mercurial repository here (.hg not found)!
abort: no suitable response from remote hg!
Run Code Online (Sandbox Code Playgroud)
我也可以直接进入machinex.com.
任何帮助是极大的赞赏.
想象一下用正则表达式捕获输入:
2.1_3_4
3.2.1
3.2.1.RELEASE
3.2.1.SNAPSHOT
Run Code Online (Sandbox Code Playgroud)
数字和点很容易得到
([0-9\._]+)
Run Code Online (Sandbox Code Playgroud)
但是你怎么捕获那个加上"RELEASE"或"SNAPHOT"或者没有?
我玩或操作员玩无济于事......
([0-9\._]+RELEASE||SNAPSHOT) // no worky
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这是一个很好的正则表达式测试器:http://java-regex-tester.appspot.com/
我正在尝试使用rust的条件编译功能,但它根本不起作用.我正在尝试使用它来在默认库和另一个库之间切换,使用它cfg来重新导出两个不同子模块中的一个,具体取决于是否设置了功能标志.代码:
lib.rs:
pub mod ffi;
#[cfg(ffiv1)]
mod ffiv1;
#[cfg(not(ffiv1))]
mod ffiv2;
#[test]
fn test_ffi_struct() {
let _fs = ffi::FFIStruct{ x: 42};
}
#[cfg(ffiv1)]
#[test]
fn test_v1() {
println!("v1 enabled");
}
Run Code Online (Sandbox Code Playgroud)
ffi.rs:
//re-export as ffi::FFIStruct
#[cfg(ffiv1)]
pub use ffiv1::FFIStruct;
#[cfg(not(ffiv1))]
pub use ffiv2::FFIStruct;
Run Code Online (Sandbox Code Playgroud)
ffiv1.rs:
pub struct FFIStruct {
pub x: i32,
y: IShouldFail
}
Run Code Online (Sandbox Code Playgroud)
ffiv2.rs:
pub struct FFIStruct {
pub x: i64
}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml:
[features]
ffiv1 = []
Run Code Online (Sandbox Code Playgroud)
使用默认的cargo build/ test,这构建并按预期工作,一切正常.
有了cargo build --features ffiv1 …
这是 Kadane 算法的 Java 实现,用于找到具有最大总和的连续子数组的总和。
static int maxSum(int[] arr) {
int maxEndingHere = arr[0];
int maxGlobal = arr[0];
for (int i = 1; i < arr.length; i++) {
maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]);
maxGlobal = Math.max(maxGlobal, maxEndingHere);
}
return maxGlobal;
}
Run Code Online (Sandbox Code Playgroud)
这只是返回总和。我想要实际的子阵列。不过,这些信息似乎丢失了。我尝试在本地最大值重置时更新开始索引,并在全局最大值更新时更新结束索引,但在这种情况下失败:
int[] arr = {-57, -10000, -1, -4, -45, -6, -9, -19, -16, -17};
Run Code Online (Sandbox Code Playgroud)
注意这里有一个类似的问题:How to return maximum sub array in Kadane's algorithm?
但据我所知,在总和为负的情况下,每个答案都会失败。
自 Epoch 午夜(例如 2021 年 2 月 13 日 00:00:00)以来,我试图以 nanos 获取今天的日期。
使用 GregorianCalendar/Date 似乎可以得到正确的结果。
我使用 Instant/ChronoUnit 的方式是明天午夜(例如 2021 年 2 月 14 日 00:00:00)。
这里有什么问题?
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.TimeZone;
class scratch {
public static void main(String[] args) {
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.setTimeZone(TimeZone.getTimeZone("UTC"));
long dateNanos = c.getTimeInMillis() * 1000000L;
Instant i = Instant.now().truncatedTo(ChronoUnit.DAYS);
long instantNanos = ChronoUnit.NANOS.between(Instant.EPOCH, i);
System.out.println("dateMillis = …Run Code Online (Sandbox Code Playgroud)