Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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中的try catch?_Java - Fatal编程技术网

检查数组中的位置是否已填充java中的try catch?

检查数组中的位置是否已填充java中的try catch?,java,Java,例如,数组[1,1]是1,如果用户想在数组[1,1]中输入一个数字,程序会告诉用户数组[1,1]已经填充了数字,并告诉用户将数字放在其他地方,我必须使用try-catch解决该问题 我将在我的简单的tic-tac-toe游戏中使用它将你的数组设置为final,这样你可以分配一次值,数组值就不能再初始化了。你可以使用对象键入你想要的数组来存储数字,数字也可以是零或不是零正/负对象类型数组有助于检查数组位置是否为空,因为对象的默认值为空 检查这个 class ArrayTest{ //Your Ar

例如,数组[1,1]是1,如果用户想在数组[1,1]中输入一个数字,程序会告诉用户数组[1,1]已经填充了数字,并告诉用户将数字放在其他地方,我必须使用try-catch解决该问题


我将在我的简单的tic-tac-toe游戏中使用它

将你的数组设置为
final
,这样你可以分配一次值,数组值就不能再初始化了。

你可以使用
对象
键入你想要的数组来存储数字,数字也可以是零或不是零正/负<代码>对象类型数组有助于检查数组位置是否为空,因为对象的默认值为空

检查这个

class ArrayTest{
//Your Array
public static Object myArray[][] = new Object[2][2];

public static void main(String args[]){
    //set myArray[1][1] to some value to satisfy your question.
    myArray[1][1] = 10;
    myArray[1][0] = null;
    //Now you wants to take input from user and as per logic it should be assign to the array location which is empty or not used.
    //take input from user
    //.......
    //.......
    int userInput = 11;
    // say array location 1,0 is empty, asign it to myArray[1][0]
    int row = 1, col = 0;
    //finding empty location
    //if isNull result is true, it means array location is empty else not empty.
    if(isNull(row,col)){
        myArray[row][col] = (Object)userInput;
    }else{
        try{
            int[][] newRow_COl = findNewLocation();
            if(newRow_COl == null){
                System.out.println("Array is full");
            }else{
                myArray[newRow_COl[0][0]][newRow_COl[0][1]] = userInput;
            }
        }catch(Exception e){
            //catch caused exception.
        }
    }
}

//This function wll return true if the array location is empty else false.
private static Boolean isNull(int row,int col){
    System.out.println(myArray[0][0]);
    if(myArray[0][0] == null)
        return true;
    else
        return false;
}
//This function will return not-null array if the logic found any empty location.
//else it will return null.
private static int[][] findNewLocation(){
    int newLocation[][] = null;
    //.....
    //logic to find new location
    //....
    //....
    //if you could find the new location
    newLocation = new int[1][1];
    newLocation[0][0] = 1; //new row location
    newLocation[1][1] = 1; //new col location
    //....

    //if you dont have any new location then
    //return newLocation as it is null

    return newLocation;
}

}

试试看
/
抓球
不适合这样做。使用
if
。不应该真正使用
try/catch
来驱动程序逻辑使用
try
catch
是作业的实际要求,或者是你想出的东西?如果数组已填充,使用
if
抛出
RuntimeException
的子类,可能是您自己定制的
ArrayFilledException
,然后使用
try/catch
捕捉它。请注意,这实际上不是好的做法;我之所以推荐这样做,是因为您似乎需要使用
try/catch