简体   繁体   English

如果超出范围,则返回空字符串

[英]Return empty string if out of bounds

How would I get my program to return an empty set if a number out of bounds is entered? 如果输入了超出范围的数字,我如何让我的程序返回一个空集? The class at the bottom is only a tester, so if someone were to change Jezebel to row 7, how would I keep it from crashing? 底部的课程仅是测试人员,因此如果有人将Jezebel更改为第7行,我如何避免它崩溃? Being that there are only six rows. 由于只有六行。

import java.util.Arrays;

public class Classroom {

private String[][] desks;
private String[] students = {"Todd", "Harry", "James", "Bob", "Michael",
    "Fred", "Andy", "Jessica", "Kara", "Ed", "Jane", "", "Dennis", "Dwight",
    "Sandy", "", "Toby", "", "Sara", "", "Randy", "", "", "", "", "John", "",
    "", "", "Lindsay", "", "", "", "", "", "Marie", "", "", "", "",
    "David", "", "",};

// the number of row is desks.length
// the number of column is desks[0].length 
public Classroom(int row, int column) {
    int i = 0;
    desks = new String[row][column];

    for (int r = 0; r < row; r++) {
        for (int c = 0; c < column; c++) {
            desks[r][c] = students[i++];
            //Arrays.fill(desks[r][c], students[i++]);
        }
    }
}

public void setDesks(String[][] desks) {
    this.desks = desks;
}

public boolean isDeskTaken(int row, int column) {
    boolean taken = false;
    if (desks[row][column] == "") {
        taken = true;
    }
    if (desks[row][column].isEmpty()) {
        taken = false;
    }
    return taken;
}

public String getRow(int row) {
    String name = Arrays.toString(desks[row]);
    return name;
}

public String getStudent(int row, int column) {
    return desks[row][column];
}

public boolean placeStudent(int row, int column, String name) {
    boolean placed = false;

    // call isDeskTaken(row, column) if not
    if (desks[row][column].isEmpty()) {
        desks[row][column] = name;
        return true;
    }
    return placed;
} // places student if the seat is empty

public static void main(String[] args) {
    Classroom classroom = new Classroom(6, 5);

    System.out.println("getRow(2)" + classroom.getRow(2));
    System.out.println("placeStudent(7,1) " + classroom.placeStudent(7, 1, "Jezebel"));
    System.out.println("placeStudent(4,0) " + classroom.placeStudent(4, 0, "John"));
    System.out.println("Student " + classroom.getStudent(4, 1) + " has been placed in the 4th row and 1st column");
}

} }

You can check the length of an array before you try and index it, like this: 您可以在尝试对数组进行索引之前检查其长度,如下所示:

String[] myArray = new String[5]; // an array with indexes [0] to [4]

// ...

// at some point we might try to access the element at index [5]
// remembering that we only have indexes [0] -> [4] in a 5 element array
// so we first check that the array is of sufficient length

if(myArray.length >= 6) {
    return myArray[5]; // 6th element
} else {
    return "";
}

Just to re-iterate, the length of the array is 5 elements, but the indexes of those elements are 0, 1, 2, 3, 4. 再次重申,数组的长度为5个元素,但这些元素的索引为0、1、2、3、4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM