我有一个自定义类。
但是即使进行了类型转换,上述代码的输出结果仍然不正确。
class StackInfo {
int start, size, capacity;
}
为了验证包含此类信息的数组中元素的总数,我有:
private int numberOfElements(StackInfo[] info) {
int size = 0;
for (StackInfo si : info) {
size += si.size;
}
return size;
}
我尝试使用流将其转换为以下内容:
private int numberOfElements(StackInfo[] info) {
return (int) Arrays.stream(info).map(s -> s.size).count();
// ^^
// to convert long
}
但是即使进行了类型转换,上述代码的输出结果仍然不正确。
info.length
呢? - Kartik