我有一个静态方法,它接受参数Stream<Double> stream
.来自a arraylist.stream()
或Arrays.stream(array)
.
该方法的工作是返回可被三整除的所有整数的总和.
return stream.filter(i -> i.intValue() % 3 == 0).mapToInt(i -> i.intValue()).sum()
此方法有效,但IntelliJ建议如下:
此检查报告lambda,可以用方法引用替换.
我不太熟悉方法引用,尤其是使用类名方案的引用实例方法.
我尝试了下面的错误.
stream.filter(i -> i.intValue() % 3 == 0).mapToInt(Integer::intValue).sum()
有什么建议?
string="file()(function)(hii)out(return)(byee)"
Run Code Online (Sandbox Code Playgroud)
对于这个字符串,我需要像这样的输出
file()()()out()()
Run Code Online (Sandbox Code Playgroud)
我试过这个
string="file()(function)(hii)out(return)(byee)"
string1=re.sub("[\(\[].*?[\)\]]", "", string)
string2=re.sub(r" ?\([^)]+\)", "", string)
print(string1)
print(string2)
Run Code Online (Sandbox Code Playgroud)
并得到类似的输出
fileout
file()out
Run Code Online (Sandbox Code Playgroud)
我想要的输出应该是这样的:
file()()()out()()
Run Code Online (Sandbox Code Playgroud) 我最近遇到了一个问题,即提出简单遗传算法的输出,该算法寻找极值为2的自变量函数f(x1,x2).我希望能够使用x1作为x,x2作为y,f作为z,并在3d空间中绘制可以旋转的点.(我目前正在使用颜色作为'z轴'在位图上绘制它.)
我应该从哪里开始?
更新:
我找到了一个用于C#的3D引擎目录,但有很多可供选择的...你能告诉我们其中任何一个是否最适合我的问题?
更新2:
感谢Cameron的建议,WPF中的3D选项似乎符合我的需求.我肯定会试一试.
我在这里重新发布他的链接:
*WPF 3D教程
*CodeProject:WPF 3D Primer
*CodeProject:WPF 3D:n的第1部分
我正在研究这种自动饮料机并在我下载Mongo 3.0.7时遇到了问题我试图运行它但是我一直收到这个错误:
我从这个网站得到了这个想法.http://yujiangtham.com/2014/05/30/build-your-very-own-drink-mixing-robot-part-2/
2016-02-02T15:56:42.585-0500 E NETWORK [initandlisten] listen():bind()失败错误号:48地址已用于套接字:0.0.0.0:27017
2016-02-02T15:56:42.585-0500 E NETWORK [initandlisten] addr已在使用2016-02-02T15:56:42.593-0500 W - [initandlisten]检测到不干净关机 - /data/db/mongod.lock不是空.
2016-02-02T15:56:42.593-0500我在initAndListen中存储[initandlisten]异常:98无法锁定文件:/data/db/mongod.lock错误:35资源暂时不可用.mongod实例是否已经运行?,终止
2016-02-02T15:56:42.593-0500 I CONTROL [initandlisten] dbexit:rc:100
我需要有关如何运行它的帮助.我用作参考的网站是 http://yujiangtham.com/2014/05/30/build-your-very-own-drink-mixing-robot-part-2/
我有这样的代码
BigDecimal withoutTax, tax, withTax, totalPrice;
totalPrice = new BigDecimal(0.0);
BigDecimal amount = new BigDecimal(String.valueOf(table.getValueAt(table.getSelectedRow(), 3)).replace(",", "."));
BigDecimal price = new BigDecimal(String.valueOf(table.getValueAt(table.getSelectedRow(), 4)).replace(",", "."));
withoutTax = amount.multiply(price, new MathContext(5));
table.setValueAt(withoutTax.toPlainString(), table.getSelectedRow(), 5);
tax = withoutTax.multiply(new BigDecimal(0.23), new MathContext(2));
table.setValueAt(tax.toPlainString(), table.getSelectedRow(), 7);
withTax = withoutTax.add(tax, new MathContext(5));
table.setValueAt(withTax.toPlainString(), table.getSelectedRow(), 8);
totalPrice.add(withTax, new MathContext(5));
paymentNum.setText(String.valueOf(totalPrice.toPlainString()));
Run Code Online (Sandbox Code Playgroud)
为什么我收到的totalPrice.add
是在withoutTax.add
工作正常的情况下被忽略了?
我试图在java中测试@Override注释.首先,我overloadedMethod
在Overload
类中重载,有和没有参数.
public class Overload {
//overloaded method with and without param
public void overloadedMethod(){
System.out.println("overloadedMethod(): No parameter");
}
public void overloadedMethod(String s){
System.out.println("overloadedMethod(): String");
}
}
Run Code Online (Sandbox Code Playgroud)
然后Override
是该类的子Overload
类.我试图覆盖其中一个overloadedMethod
.该@Override
注释被添加到确保这种方法是否正确重写.
public class Override extends Overload{
//now I want to override
@Override
public void overloadedMethod(String s){
System.out.println("overloadedMethod(): String is overrided");
}
/**
* MAIN
* @param args void
*/
public static void main(String[] args){
Override myOverride=new Override();
//test
myOverride.overloadedMethod();
myOverride.overloadedMethod("Hello"); …
Run Code Online (Sandbox Code Playgroud) 我有这样的界面:
public interface Listener {
void onA();
void onB();
void onC();
}
Run Code Online (Sandbox Code Playgroud)
并且有一个监听器/观察者列表:
List<Listener> listeners = new ArrayList<Listener>();
Run Code Online (Sandbox Code Playgroud)
我怎样才能轻松地通知所有的听众,有A,B,C通过发生Listener.onA()
,Listener.onB()
,Listener.onC()
?
我是否必须至少三次在所有侦听器上复制粘贴迭代?
在C++中我会创建这样一个函数:
void Notify(const std::function<void(Listener *listener)> &command) {
for(auto &listener : listeners) {
command(listener);
}
}
Run Code Online (Sandbox Code Playgroud)
并为每个方法传递lambda:
Notify([](Listener *listener) {listener->onA();});
Run Code Online (Sandbox Code Playgroud)
要么
Notify([](Listener *listener) {listener->onB();});
Run Code Online (Sandbox Code Playgroud)
要么
Notify([](Listener *listener) {listener->onC();});
Run Code Online (Sandbox Code Playgroud)
Java中是否有类似的方法?
在不区分大小写的情况下,如何使用.contains方法搜索ArrayList?我试过了.containsIgnoreCase,但是发现IgnoreCase方法仅适用于字符串。
这是我尝试创建的方法:
private ArrayList<String> Ord = new ArrayList<String>();
public void leggTilOrd(String ord){
if (!Ord.contains(ord)){
Ord.add(ord);
}
}
Run Code Online (Sandbox Code Playgroud) 这是给出错误的代码 -
try(StringWriter stringWriter = new StringWriter()) {
Run Code Online (Sandbox Code Playgroud)
IDE 抱怨来自自动关闭资源的未处理异常:java.io.IOException 是否有必要在此处在构造函数中传递某些内容?
我对 Scott Meyer 的书“Effective Modern C++”的第 24 条感到很兴奋。他提到了编写 C++14 lambda 来记录任意函数调用所用时间的可能性。
我还处于学习 C++14 特性的早期阶段。我的尝试(Main.cpp)看起来像这样测量成员函数调用的时间:
#include <chrono>
#include <iostream>
auto measure = [](auto&& function, auto&&... parameters) -> decltype(function)
{
const std::chrono::steady_clock::time_point startTimePoint =
std::chrono::steady_clock::now();
const auto returnValue = std::forward<decltype(function)>(function)(
std::forward<decltype(parameters)>(parameters)...);
const std::chrono::steady_clock::time_point stopTimePoint =
std::chrono::steady_clock::now();
const std::chrono::duration<double> timeSpan = std::chrono::duration_cast<
std::chrono::duration<double>>(stopTimePoint - startTimePoint);
std::cout << "Computation took " << timeSpan.count()
<< " seconds." << std::endl;
return returnValue;
};
class Test
{
public:
int computation(double dummy)
{
std::cout << "Received " << …
Run Code Online (Sandbox Code Playgroud)