import java.lang.reflect.Array;
/**
* A 2D cartesian plane implemented as with an array. Each (x,y) coordinate can
* hold a single item of type <T>.
*
* @param <T> The type of element held in the data structure
*/
public class ArrayCartesianPlane<T> implements CartesianPlane<T> {
private int minimumX;
private int maximumX;
private int minimumY;
private int maximumY;
private T[][] list;
/**
* Constructs a new ArrayCartesianPlane object with given minimum and
* maximum bounds.
*
* Note that these bounds are allowed to be negative.
*
* @param minimumX A new minimum bound for the x values of
* elements.
* @param maximumX A new maximum bound for the x values of
* elements.
* @param minimumY A new minimum bound for the y values of
* elements.
* @param maximumY A new maximum bound for the y values of
* elements.
* @throws IllegalArgumentException if the x minimum is greater
* than the x maximum (and resp. with y min/max)
*/
public ArrayCartesianPlane(int minimumX, int maximumX, int minimumY,
int maximumY) throws IllegalArgumentException {
// TODO: implement the constructor
this.minimumX = minimumX;
this.minimumY = minimumY;
this.maximumX = maximumX;
this.maximumY = maximumY;
this.list = new T[maximumX-minimumX][maximumY-minimumY];
}
/**
* Add an element at a fixed position, overriding any existing element
* there.
*
* @param x The x-coordinate of the element's position
* @param y The y-coordinate of the element's position
* @param element The element to be added at the indicated
* position
* @throws IllegalArgumentException If the x or y value is out of
* the grid's minimum/maximum bounds
*/
@Override
public void add(int x, int y, T element) throws IllegalArgumentException {
list[x][y] = element;
}
/**
* Returns the element at the indicated position.
*
* @param x The x-coordinate of the element to retrieve
* @param y The y-coordinate of the element to retrieve
* @return The element at this position, or null is no elements exist
* @throws IndexOutOfBoundsException If the x or y value is out of
* the grid's minimum/maximum bounds
*/
@Override
public T get(int x, int y) throws IndexOutOfBoundsException {
return (T)(list[x][y]);
}
/**
* Removes the element at the indicated position.
*
* @param x The x-coordinate of the element to remove
* @param y The y-coordinate of the element to remove
* @return true if an element was successfully removed, false if no element
* exists at (x, y)
* @throws IndexOutOfBoundsException If the x or y value is out of
* the grid's minimum/maximum bounds
*/
@Override
public boolean remove(int x, int y) throws IndexOutOfBoundsException {
if (list[x][y] == 0) {
return false;
}
}
/**
* Removes all elements stored in the grid.
*/
@Override
public void clear() {
}
/**
* Changes the size of the grid. Existing elements should remain at the
* same (x, y) coordinate If a resizing operation has invalid dimensions or
* causes an element to be lost, the grid should remain unmodified and an
* IllegalArgumentException thrown
*
* @param newMinimumX A new minimum bound for the x values of
* elements.
* @param newMaximumX A new maximum bound for the x values of
* elements.
* @param newMinimumY A new minimum bound for the y values of
* elements.
* @param newMaximumY A new maximum bound for the y values of
* elements.
* @throws IllegalArgumentException if the x minimum is greater
* than the x maximum (and resp. with y min/max) or if an element
* would be lost after this resizing operation
*/
@Override
public void resize(int newMinimumX, int newMaximumX, int newMinimumY, int newMaximumY) throws IllegalArgumentException {
}
// TODO: you are to implement all of ArrayCartesianPlanes's methods here
}
定义泛型数组,怎么创建一个已知大小的二维数组
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 风丶飞 2020-08-17 16:04关注
public class Text { public static void main(String[ ] args) { Test test = new Test(4,3,2,1); System.out.println(test.getList().length); System.out.println(test.getList()[0].length); } } class Test{ private int minX; private int minY; private int maxX; private int maxY; private T[][] list; public Test(int minX, int minY, int maxX, int maxY) { this.minX = minX; this.minY = minY; this.maxX = maxX; this.maxY = maxY; this.list = new T[minX-minY][maxX-maxY]; } } 结果 1 1 Process finished with exit code 0
这样试试
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 ensp,用局域网解决
- ¥15 Python语言实验
- ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
- ¥20 电脑重启停留在grub界面,引导出错需修复
- ¥15 matlab透明图叠加
- ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
- ¥150 计算DC/DC变换器平均模型中的参数mu
- ¥25 C语言代码,大家帮帮我
- ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
- ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)