有人对如何获取指示该街区是否有空停车位的函数的值有任何想法吗?我正在使用队列,并且尝试了函数nFree()
和spaceIndex()
,但我发现这两个函数未为类型定义
希望我理解您的问题,以下是可能返回所有停车空位的代码。
公共类停车场{
static int parkingSize = 10;
static Object[] object = new Object[parkingSize];
public static void main(String[] args) {
add("java", 10);
System.out.println(checkEmptyParkingSpot());
emptyParkingSpot(10);
System.out.println(checkEmptyParkingSpot());
}
// add the object in specicif location
public static void add(String info, int position) {
// if the position does not exist an erro will be thow
if (position > object.length ) {
throw new RuntimeException("The possition indicated does not exist in the park");
}
if (object[position - 1] != null) {
throw new RuntimeException("The possition is ocupied");
}
object[position - 1] = info;
}
// to remove an object from the park and free the space
public static void emptyParkingSpot(int position) {
if (position > object.length ) {
throw new RuntimeException("The possition indicated does not exist in the park");
}
if (object[position - 1] == null) {
throw new RuntimeException("The possition isalready empty");
}
object[position - 1] = null;
}
// lists the empty spot
public static String checkEmptyParkingSpot() {
String emptySpace = "".trim();
for (int i = 0; i < object.length; i++) {
// list the empty space
if (object[i] == null) {
emptySpace = emptySpace + (i + 1) + "\n";
}
}
// if there is no space the message will be park full
if (emptySpace.isEmpty()) {
emptySpace = "Park is Full";
}
return emptySpace;
}
}