Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何组合所有类似的数组循环方法?Java 1.8_Java - Fatal编程技术网

如何组合所有类似的数组循环方法?Java 1.8

如何组合所有类似的数组循环方法?Java 1.8,java,Java,为了学习编程,我正在用java制作一个游戏2048。游戏有一个类,其中有许多重复的片段-场迭代循环。问题是,是否有可能创建这样一个方法来接受枚举边界和可执行代码的值 类似这样的东西既用于赋值也用于比较 public void forField(int-borderX,int-borderY,?code){ 对于(int x=0;x

为了学习编程,我正在用java制作一个游戏2048。游戏有一个类,其中有许多重复的片段-场迭代循环。问题是,是否有可能创建这样一个方法来接受枚举边界和可执行代码的值

类似这样的东西既用于赋值也用于比较

public void forField(int-borderX,int-borderY,?code){
对于(int x=0;x
如果没有,您能推荐如何改进我的代码吗?谢谢你的关注

公共类字段{
私有int[][]字段;
私有整数宽度、高度;
公共字段(int-sizeX,int-sizeY){
此宽度=sizeX;
this.heigth=sizeY;
this.field=new int[sizeX][sizeY];
}
公共整数getCell(整数x,整数y){
返回此.field[x][y];
}
公共空集合单元格(int val,int x,int y){
此.field[x][y]=val;
}
//重新启动
公共无效重置(){
对于(int x=0;x
forField
中的
可以只是一个
BiConsumer
,或者如果您不喜欢装箱,您可以声明自己的
IntBiConsumer
接口:

interface IntBiConsumer {
    void accept(int i, int j);
}
forField
可以声明为:

public void forField(int borderX, int borderY, IntBiConsumer code){
    for (int x = 0; x < borderX; x++)
        for (int y = 0; y < borderY; y++)
           code.accept(x, y);
}

checkField
有点棘手,您建议的代码实际上不起作用,因为在内部循环中返回
之前,您没有检查任何内容。循环将只循环一次,并立即返回。它应该更像:

public boolean checkField(int borderX, int borderY, ??? condition, boolean default_answer){
    for (int x = 0; x < borderX; x++)
        for (int y = 0; y < borderY; y++)
            if (condition) 
                return !default_answer;
    return default_answer;
}
checkField
可以这样声明:

interface IntBiPredicate {
    boolean test(int i, int j);
}
public boolean checkField(int borderX, int borderY, IntBiPredicate condition, boolean defaultAnswer){
    for (int x = 0; x < borderX; x++)
        for (int y = 0; y < borderY; y++)
            if (condition.test()) 
                return !defaultAnswer;
    return defaultAnswer;
}

forField
中的
可以只是一个
BiConsumer
,或者如果您不喜欢装箱,您可以声明自己的
IntBiConsumer
界面:

interface IntBiConsumer {
    void accept(int i, int j);
}
forField
可以声明为:

public void forField(int borderX, int borderY, IntBiConsumer code){
    for (int x = 0; x < borderX; x++)
        for (int y = 0; y < borderY; y++)
           code.accept(x, y);
}

checkField
有点棘手,您建议的代码实际上不起作用,因为在内部循环中返回
之前,您没有检查任何内容。循环将只循环一次,并立即返回。它应该更像:

public boolean checkField(int borderX, int borderY, ??? condition, boolean default_answer){
    for (int x = 0; x < borderX; x++)
        for (int y = 0; y < borderY; y++)
            if (condition) 
                return !default_answer;
    return default_answer;
}
checkField
可以这样声明:

interface IntBiPredicate {
    boolean test(int i, int j);
}
public boolean checkField(int borderX, int borderY, IntBiPredicate condition, boolean defaultAnswer){
    for (int x = 0; x < borderX; x++)
        for (int y = 0; y < borderY; y++)
            if (condition.test()) 
                return !defaultAnswer;
    return defaultAnswer;
}