我看到有两种方法可以在数组中找到中间索引.
int middle = lowerBound + (upperBound-lowerBound)/2
int middle = (lowerBound + upperBound)/2
是一回事吗?或者它们的使用方式不同?因为我试图理解二进制搜索和合并排序.两者都必须使用它.
非常感谢.
我需要计算整数和长整数的概率.但我总是0.
int a = 234;
长b = 123453344L
float c = a/b;
如何在Java中正确使用它?
它总是抱怨:
The method add(Matrix<T>) in the type List<Matrix<T>> is not applicable for the arguments (Matrix<String>)
Run Code Online (Sandbox Code Playgroud)
在Extractor类的行中:
matrixList.add(new Matrix<String>(attributes));
Run Code Online (Sandbox Code Playgroud)
在这3个类中定义我的泛型似乎存在问题.有没有一种简单的方法来解决这个问题?尝试了不同的方式,无法弄明白.
public class Test {
public static void main(String[] args) {
Extractor<String> extractor = new Extractor<>();
extractor.extract();
}
}
class Extractor<T> {
private List<Matrix<T>> matrixList;
public Extractor() {
this.matrixList = new ArrayList<>();
}
public void extract() {
List<Attribute<String>> attributes = new ArrayList<>();
attributes.add(new Attribute<String>("Test 1"));
attributes.add(new Attribute<String>("Test 2"));
// !!!! The compiler is complaining here!
matrixList.add(new Matrix<String>(attributes));
}
public List<Matrix<T>> getList() { …Run Code Online (Sandbox Code Playgroud) 在我的测试用例中,我使用assertEquals()进行单元测试.问题是这个断言的值来自HashSet,其中顺序是随机的.因此,每次运行测试用例时,由于HashSet的字符串表示形式值不同,我的断言可能会失败.
为了解决这个问题,我想在我的代码中使用LinkedHashSet,但这似乎不是一个有效的解决方案,因为LinkedHashSet的选择只是为了适应一些测试用例.
这是单元测试中的常见情况吗?
在我的 IntelliJ 编辑器中,我有一个 test.json 文件,如下所示。第二个 json 记录显示错误消息“Json 标准只允许一个顶级值”。但这是一个有效的 JSON 文件,对吗?如何摆脱这个烦人的消息?
{"reviewerID": "A2XVJBSRI3SWDI", "asin": "0000031887", "reviewerName": "abigail", "helpful": [0, 0], "title": "Ballet Dress-Up Fairy Tutu", "url": "https://www.amazon.com/dp/0000031887", "brand": "Boutique Cutie", "reviewText": "Perfect red tutu for the price. I baught it as part of my daughters Halloween costume and it looked great on her.", "overall": 5.0, "summary": "Nice tutu", "unixReviewTime": 1383523200, "reviewTime": "11 4, 2013"}
{"reviewerID": "A2G0LNLN79Q6HR", "asin": "0000031887", "reviewerName": "aj_18 \"Aj_18\"", "helpful": [1, 1], "title": "Ballet Dress-Up Fairy Tutu", "url": "https://www.amazon.com/dp/0000031887", …Run Code Online (Sandbox Code Playgroud) 在My IntelliJ中的我的自定义VM选项中,我的idea64.exe.vmoptions中有"-ea".但为什么这个报告断言错误:
int a = 1;
int b = 2;
assert(a==b)
Run Code Online (Sandbox Code Playgroud) 例如,给定此图:
gremlin> graph = TinkerFactory.createModern() (1)
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal(standard()) (2)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').out('knows').values('name') (3)
Run Code Online (Sandbox Code Playgroud)
我想从控制台打印出该图形的PNG文件.文档没有说明如何做到这一点.提前致谢.
我的电脑有8个内核和64G内存.我的使用多线程的方式如下吗?它处理来自每个文档的一行(filePath)并将结果存储到Document列表中,最后返回.
问题是,当设置'4'线程时,我没有看到它运行得更快.花在测试数据上的时间总是与单线程运行所需的时间相同.我使用Callable的方式有什么问题吗?
List<Document> processDocs(String filePath) {
ExecutorService pool = Executors.newFixedThreadPool(4);
List<Document> processedDocs = new ArrayList<>();
try {
br = new BufferedReader(IOUtils.fileReader(filePath));
String line = null;
int docNo=0;
while ((line = br.readLine()) != null) {
line = line.trim();
Callable<Document> callable = new NLPTextThread(line, ++docNo);
Future<Document> future = pool.submit(callable);
try {
processedDocs.add(future.get());
} catch (InterruptedException e) {
log.error("InterruptedException " + line);
} catch (ExecutionException e) {
log.error("ExecutionException: " + line);
e.printStackTrace();
}
}
pool.shutdown();
return processedDocs;
}
Run Code Online (Sandbox Code Playgroud)
编辑:关于'docNo'变量的线程安全性的另一个问题.我想将doc序列号传递给Callable.在这种情况下,"docNo"变量的线程安全吗?
例如,我有一个私有成员初始化为String str ="A".程序结束后,我希望str成为"B".然后,第二天当我运行相同的程序时,str的值是"B",而不是"A".
谢谢.
123|abc|dog
4a6|adc|cat
...
Run Code Online (Sandbox Code Playgroud)
我想摆脱第一个'|'的所有角色 在VIM的每一行.结果将是:
abc|dog
adc|cat
...
Run Code Online (Sandbox Code Playgroud) java ×7
arrays ×1
generics ×1
json ×1
neo4j ×1
python ×1
tinkerpop ×1
tinkerpop3 ×1
unit-testing ×1
vim ×1