【问题标题】:Checking a position's bounds before accessing it在访问位置之前检查位置的边界
【发布时间】:2018-01-19 02:50:50
【问题描述】:

我正在尝试打印一个二维数组并确定起始元素的北、南、东和西元素。我已经用随机整数填充了数组,但是如果方向超出范围而不是使整个程序崩溃,则需要帮助打印该方向的“-1”

import java.util.Scanner;

public class GetNeighbors
{

    public static void printRow(int[]row) 
        {
            for (int i : row) {
            System.out.print(i);
            System.out.print("\t");
        }
        System.out.println();
    }

    public static void main(String[]args)
    {
        Scanner kb=new Scanner(System.in);

        System.out.println("Please enter a number of rows");
        int r=kb.nextInt();
        System.out.println("Please enter a number of columns");
        int c=kb.nextInt();

        int[][]spot=new int[r][c];

        arrayWork(spot);
        directionWork(spot);

    }

    public static void arrayWork(int[][]spot)
    {   
        for(int i=0;i<spot.length;i++)
        {
            for(int j=0;j<spot[i].length;j++)
            {
                spot[i][j]=(int)(Math.random()*1001);
            }
        }
        for(int[]row:spot) 
            {
            printRow(row);
            }
    }

    public static void directionWork(int[][]spot)
    {
        Scanner kb=new Scanner(System.in);

        System.out.println("Please choose a row for an element");
        int row=kb.nextInt();
        System.out.println("Please choose a column for an element");
        int col=kb.nextInt();

        System.out.println("Requested neighbors for element at row "+row+", col "+col+" "+"("+spot[row][col]+")");

        System.out.print("North: "+spot[row-1][col]);
        System.out.print(" South: "+spot[row+1][col]);
        System.out.print(" East: "+spot[row][col+1]);
        System.out.print(" West: "+spot[row][col-1]);
    }
}

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    为避免重复一堆条件,您可以创建一个方法来在检索值时检查行和列是否在界限内:

    public static int getElement(int[][] spot, int row, int col) {
        if (row < spot.length && spot.length > 0 && row >= 0 && col < spot[row].length && spot[row].length > 0 && col >= 0) {
            return spot[row][col];
        } else {
            return -1;
        }
    }
    

    然后将您的打印语句更改为调用它:

    System.out.println("Requested neighbors for element at row "+row+", col "+col+" "+"("+getElement(spot, row, col)+")");
    
    System.out.print("North: "+ getElement(spot, row - 1, col));
    System.out.print(" South: " + getElement(spot, row + 1, col));
    System.out.print(" East: " + getElement(spot, row, col + 1));
    System.out.print(" West: "+ getElement(spot, row, col - 1));
    

    【讨论】:

    • 必须包含另外两个条件。 If [row+1] &gt;= spot.length... 也用于专栏。
    • @zlakad 不,你只需要检查正在访问的索引。
    • System.out.println("Requested neighbors for element at row "+row+", col "+col+" "+"("+spot[row][col]+")"); 也需要检查。
    • @user27149 谢谢,已添加。
    • 东:getElement(spot, row, col + 1)。如果col 是最左边的列怎么办? (这是一个例子)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多