我是Java 8流的新手,我想知道是否有方法执行forEach/map调用方法返回a byte并接受intas参数.
例:
public class Test {
private byte[] byteArray; // example of byte array
public byte getByte(int index) {
return this.byteArray[index];
}
public byte[] getBytes(int... indexes) {
return Stream.of(indexes)
.map(this::getByte) // should return byte
.collect(byte[]::new); // should return byte[]
}
}
Run Code Online (Sandbox Code Playgroud)
你可能猜到,getBytes方法不起作用."int[] cannot be converted to int"可能某个地方缺少预告,但个人无法弄明白.
然而,这是一种工作,老式的方法,我想重写为Stream.
byte[] byteArray = new byte[indexes.length];
for ( int i = 0; i < byteArray.length; i++ ) {
byteArray[i] = this.getByte( indexes[i] …Run Code Online (Sandbox Code Playgroud) 我无法引用我MyClass班级的成员函数.请把这个抽象视为我的问题.
class MyClass {
public:
DWORD fun_32(_In_ DWORD64 a64bitparam); //wants DWORD64 returns DWORD
DWORD64 fun_64(_In_ DWORD64 a64bitparam); //wants DWORD64 returns DWORD64
DWORD64 fun(_In_ DWORD64 a64bitparam);
private:
BOOLEAN use64;
};
Run Code Online (Sandbox Code Playgroud)
一个fun(DWORD64)实现:
DWORD64 MyClass::fun(DWORD64 a64bitparam) {
std::function<DWORD64(DWORD64)> myReference;
myReference = this->use64 ? &MyClass::fun_64 : &MyClass::fun_32;
}
Run Code Online (Sandbox Code Playgroud)
该示例将不会编译为"操作数类型不兼容".某种程度上该程序无法投射DWORD到DWORD64.
但是,这个确实如此,但看起来很难看.(IMO)
std::function<DWORD64(DWORD64)> myReference;
myReference = [this](DWORD64 a) -> DWORD64 { return use64 ? fun_64(a) : fun_32(a);};
Run Code Online (Sandbox Code Playgroud)
有人可以简单地解释一下我在方法参考中做错了什么吗?