我遇到的问题是将这段代码转换为Java8流。基本上我有一个死的(假)或活的(真)单元格列表,我需要检查给定的邻居有多少细胞
private static int checkMyLivingNeighbours(Cell cell,List<Cell> currentcells){
int neighbours = 0;
for (int y = cell.getY() - 1; y <= cell.getY() + 1; y++) {
for (int x = cell.getX() - 1; x <= cell.getX() + 1; x++) {
if(x!=cell.getX() || y!=cell.getY()){
for (Cell nowcell : currentcells) {
if (nowcell.getX() == x && nowcell.getY() == y) {
if (nowcell.getStatus()) {
neighbours++;
}
}
}
}
}
}
return neighbours;
}
我尝试过这样的事情
private static void checkaliveneighbours(Cell cell,List<Cell> generation){
generation.stream().forEach(n->IntStream.range(cell.getY()-1,cell.getY()+1).
forEach(y -> IntStream.range(cell.getX()-1,cell.getX()+1)
.forEach(x->{if(n.getX()==x && n.getY()==y && n.getStatus())System.out.println(n.getDisplaychar());})));;//
}
我称之为
checkaliveneighbours(generation.get(0),generation);
所以我确实获得了活CELL的打印但我实际上需要传入CELL周围的活CELLS的总数nr而不仅仅是传入的单元格是否存活。因此,如何响应地(基于传入的单个单元)限制流(仅周围的单元)中的元素数量的问题。
这是细胞类
public class Cell {
private int x;
private int y;
private boolean alive;
public Cell(){}
public Cell(String x, String y, boolean alive ) {
this.x = Integer.valueOf(x);
this.y = Integer.valueOf(y);
this.alive = alive;
}
public int getX() {
return x;
}
public void setX(String x) {
this.x = Integer.valueOf(x);
}
public int getY() {
return y;
}
public void setY(String y) {
this.y = Integer.valueOf(y);
}
public boolean getStatus() {
return alive;
}
public void setStatus(boolean status) {
this.alive = status;
}
public char getDisplaychar() {
if(getStatus())
return 'X';
else
return '.';
}
}
答案 0 :(得分:2)
如果我理解正确,通过&#34;限制&#34;你的意思是使用#import "UIImage+Zoom.h"
@implementation UIImage (Zoom)
/*
* @brief resizeImage
* @param newsize the dimensions(pixel) of the output image
*/
- (UIImage*)resizeImageToSize:(CGSize)newSize resizeMode:(SvResizeMode)resizeMode
{
CGRect drawRect = [self caculateDrawRect:newSize resizeMode:resizeMode];
UIGraphicsBeginImageContext(newSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, CGRectMake(0, 0, newSize.width, newSize.height));
CGContextSetInterpolationQuality(context, 0.8);
[self drawInRect:drawRect blendMode:kCGBlendModeNormal alpha:1];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
// caculate drawrect respect to specific resize mode
- (CGRect)caculateDrawRect:(CGSize)newSize resizeMode:(SvResizeMode)resizeMode
{
CGRect drawRect = CGRectMake(0, 0, newSize.width, newSize.height);
CGFloat imageRatio = self.size.width / self.size.height;
CGFloat newSizeRatio = newSize.width / newSize.height;
switch (resizeMode) {
case enSvResizeScale:
{
// scale to fill
break;
}
case enSvResizeAspectFit: // any remain area is white
{
CGFloat newHeight = 0;
CGFloat newWidth = 0;
if (newSizeRatio >= imageRatio) { // max height is newSize.height
newHeight = newSize.height;
newWidth = newHeight * imageRatio;
}
else {
newWidth = newSize.width;
newHeight = newWidth / imageRatio;
}
drawRect.size.width = newWidth;
drawRect.size.height = newHeight;
drawRect.origin.x = newSize.width / 2 - newWidth / 2;
drawRect.origin.y = newSize.height / 2 - newHeight / 2;
break;
}
case enSvResizeAspectFill:
{
CGFloat newHeight = 0;
CGFloat newWidth = 0;
if (newSizeRatio >= imageRatio) { // max height is newSize.height
newWidth = newSize.width;
newHeight = newWidth / imageRatio;
}
else {
newHeight = newSize.height;
newWidth = newHeight * imageRatio;
}
drawRect.size.width = newWidth;
drawRect.size.height = newHeight;
drawRect.origin.x = newSize.width / 2 - newWidth / 2;
drawRect.origin.y = newSize.height / 2 - newHeight / 2;
break;
}
default:
break;
}
return drawRect;
}
来过滤邻居。然后你要总结所有生活的邻居。我首先定义一个方法,它将返回指定的单元格是否是相邻单元格:
Stream.filter()
然后您可以轻松过滤列表并计算总和,如下所示:
private static boolean isNeighbor(final Cell cell, final Cell candidate) {
for (int y = cell.getY() - 1; y <= cell.getY() + 1; y++) {
for (int x = cell.getX() - 1; x <= cell.getX() + 1; x++) {
if (x != cell.getX() || y != cell.getY()) {
if (candidate.getX() == x && candidate.getY() == y) {
return true;
}
}
}
}
return false;
}
修改:如果您正在为int sum = generation.stream()
.filter(c -> isNeighbor(cell, c))
.mapToInt(c -> c.getStatus() ? 1 : 0)
.sum();
寻找纯Java 8解决方案:
isNeighbor