中级实训 Part5 问题回答

Step1:The AbstractGrid Class

Set 10

  • 1.Where is the isValid method specified? Which classes provide an implementation of this method?
    回答: isValid 方法在Grid接口中指定了,然后在BoundedGrid 和 UnboundedGrid 两个类中分别实现。

  • 2.Which AbstractGrid methods call the isValid method? Why don’t the other methods need to call it?
    回答: getValidAdjacentLocations 方法使用了isValid 方法。
    而getEmptyAdjacentLocations 和getOccupiedAdjacentLocations 方法通过调用getValidAdjacentLocations 方法来间接调用isValid 方法;
    方法getNeighbors不直接调用isValid。 它调用getOccupiedAdjacentLocations,后者调用getValidAdjacentLocations,然后再调用isValid。

  • 3.Which methods of the Grid interface are called in the getNeighbors method? Which classes provide implementations of these methods?
    回答:方法getNeighbors调用Grid方法的get和getOccupiedAdjacentLocations。
    AbstractGrid类实现getOccupiedAdjacentLocations方法。 get方法在BoundedGrid和UnboundedGrid类中实现。
    在这里插入图片描述

  • 4.Why must the get method, which returns an object of type E, be used in the getEmptyAdjacentLocations method when this method returns locations, not objects of type E?
    回答: get方法返回对存储在给定位置网格中对象的引用;如果不存在对象,则返回null。 getEmptyAdjacentLocations调用get方法并测试结果以查看结果是否为null。 如果返回null,则该位置为“空”并添加到列表中。 调用get方法是测试给定位置是否为空或已占用的唯一方法。
    在这里插入图片描述

  • 5.What would be the effect of replacing the constant Location.HALF_RIGHT with Location.RIGHT in the two places where it occurs in the getValidAdjacentLocations method?
    回答: 可能的有效相邻位置的数量将从八个减少到四个。 因为有效位置的方向从八个减少到了四个(东南西北)

Step2:The BoundedGrid Class

Set 11

  • 1.What ensures that a grid has at least one valid location?
    回答: 如果rows <= 0或cols <= 0,BoundedGrid的构造函数将引发IllegalArgumentException。以此确保至少有一个有效位置。
    在这里插入图片描述

  • 2.How is the number of columns in the grid determined by the getNumCols method? What assumption about the grid makes this possible?
    回答: 通过occupantArray[0].length
    构造函数确保每个BoundedGrid对象至少具有一行和一列。
    在这里插入图片描述

  • 3.What are the requirements for a Location to be valid in a BoundedGrid?
    回答: 位置的行值大于等于0且小于BoundedGrid的行数, 位置的列值大于等于0且小于BoundedGrid的列数。
    在这里插入图片描述

  • 4.What type is returned by the getOccupiedLocations method? What is the time complexity (Big-Oh) for this method?
    回答: 返回类型是ArrayList<//Location>。
    访问BoundedGrid中的每个位置的时间复杂度是O(r * c)。 将占用的位置将添加到ArrayList的末尾,时间复杂度是O(1)。
    在这里插入图片描述

  • 5.What type is returned by the get method? What parameter is needed? What is the time complexity (Big-Oh) for this method?
    回答: get方法返回的类型为E,这是occupantArray中存储的任何类型。
    get方法需要一个Location对象作为参数。
    通过行和列的值访问给定的二维数组,时间复杂度为O(1)。
    在这里插入图片描述

  • 6.What conditions may cause an exception to be thrown by the put method? What is the time complexity (Big-Oh) for this method?
    回答: 如果要添加对象的位置不在网格中(无效位置),则将引发IllegalArgumentException。 如果发送给put方法的对象为null,则将引发NullPointerException;
    put方法的时间复杂度为O(1)。
    在这里插入图片描述

  • 7.What type is returned by the remove method? What happens when an attempt is made to remove an item from an empty location? What is the time complexity (Big-Oh) for this method?
    回答: 返回类型是通用模板E;
    如果尝试从空位置中删除项目,则将null存储在该位置中,并返回null。 在空的位置上调用Grid类的remove方法不是错误;
    remove方法的时间复杂度为O(1)。

  • 8.Based on the answers to questions 4, 5, 6, and 7, would you consider this an efficient implementation? Justify your answer.
    回答: 是的。对于整个Grid的实现来说,唯一比较低效的地方就是getOccupiedLocations 方法需要遍历整个grid来查找被占用的位置,其余操作都是O(1)的。
    当然,这个问题也并不是不可避免的。可以通过HashMap来实现既可以访问被占用位置,也存储了没有被占用位置的BoundedGrid。

Step3:The UnboundedGrid Class

Set 12

  • 1.Which method must the Location class implement so that an instance of HashMap can be used for the map? What would be required of the Location class if a TreeMap were used instead? Does Location satisfy these requirements?
    回答: Location类必须实现hashCode和equals方法。 当调用equals方法时,hashCode方法必须在两个测试为true的位置返回相同的值。
    TreeMap要求地图的键是可比较的。 Location类实现Comparable接口。 因此,必须将compareTo方法实现,以使Location类成为非抽象类。 当调用equals方法时,对于两个测试为true的位置,compareTo方法应返回0。
    Location类满足所有这些要求。

  • 2.Why are the checks for null included in the get, put, and remove methods? Why are no such checks included in the corresponding methods for the BoundedGrid?
    回答: UnboundedGrid使用HashMap作为其数据结构将项目保存在网格中。 UnBoundedGrid的isValid方法始终返回true; 它不会检查空位置。 在Map对象中,null是键的合法值。 而在UnboundedGrid对象中,null不是有效位置。 因此,在UnboundedGrid中的get, put 和 remove方法必须检查location参数并在参数为null时引发NullPointerException。
    因为在BoundedGrid中,任何需要访问location的方法,包括get,put和remove等,都需要进行一次isValid的判断,如果isValid方法中的location参数为null,则会引发NullPointerException。

  • 3.What is the average time complexity (Big-Oh) for the three methods: get, put, and remove? What would it be if a TreeMap were used instead of a HashMap?
    回答: get, put 和 remove的平均时间复杂度为O(1)。
    如果使用TreeMap代替HashMap,则平均时间复杂度将为O(log n),其中n是网格中占用位置的数量。

  • 4.How would the behavior of this class differ, aside from time complexity, if a TreeMap were used instead of a HashMap?
    回答: getOccupiedLocations方法将以不同的顺序返回被占用的位置。 因为HashMap中的键(位置)基于一个索引放置在哈希表中,该索引是通过使用键的hashCode和表的大小来计算的。 遍历keySet时访问键的顺序取决于其在哈希表中的位置。
    而TreeMap将其关键字存储在平衡的二进制搜索树中,并按有序遍历遍历该树。 keySet(位置)中的键将按照Location的compareTo方法定义的升序(主要顺序)进行访问。
    所以两者最后得到的访问顺序会与一定的变化。

  • 5.Could a map implementation be used for a bounded grid? What advantage, if any, would the two-dimensional array implementation that is used by the BoundedGrid class have over a map implementation?
    回答: 可以。如果使用HashMap来实现有界网格,则getOccupiedLocations的平均时间复杂度为O(n),其中n是网格中的项目数。
    优点可以考虑以下情况:如果BoundedGrid几乎已满,则map实现将使用更多内存,因为map会存储内容及其位置。 而二维数组仅存储内容。 通过组合每个内容的行索引和列索引来生成位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值