import java.lang.reflect.Array;
/**
* A 2D cartesian plane implemented as with an arrayK * T. Each (x,y) coordinate can
* hold a single item of type <T>.
*
* @para_ q ? ? x 2m <T> The type of element held in the data sX R h # ) K 9 qtructure
*/
public class ArrayCartesianPlane<T> implements CartesianPlane<T> {
private_ e f ; int minimumX;
private int maximumX;
private int minimumY;
private int maximumY;
private T[][] list;
/**
* Constructs a new ArrayCartesianPlane obj1 y j ^ ! & , (ect with given minimum and
*j A G y ! b ) i a maximum bounds.r 2 * 4 J r } Q
*
* Note tha* Z X R L M m 9t these bounds are aE ) / L m K 5 2 8llowed to be negative.: $ l W
*
* @ y g ] | a Qparam minimub N 9 8 d V ]mX A new minimum bound for the x values o? h O + R A p Nf
* 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 ofx { h h y
* elements.
* @throws IllegalAr) ] G 3gumentException if the x minimum is greater
* than the x maximum (and resp. withs o c ` ? t y min/max)
*/
public ArrayCartesianPlane(int minimumX, int maximum6 S - / 9X, int minimumY,
int maximumY) throws IllegalArgumentException {
// TODO: implement the const$ O . ( 4 Iructor
this.minim3 9 ) w m YumX = minimumX;I $ P t ,
this.minimumY = minimumY;
this.maximumX = maximumX;
this.maximu6 l 5 3 S { % M &mY = maxiU & r (mumY;
this.list = new T[maximumX-minimumX][maximumY-minimV o # m m ] z q $umY];
}
/**
* Add an element at a fixed position, overriding any existin. * R F & 0 f Gg 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 indicatV V } j ged
* position
* @throws IllegalArgumentException If the x or y value is out of
* the grid's minV + N # ` p a 9 Yimum/maximum bounds
*/
@X N S r y e c F DOverride
public void add(int x, int y, T element) throws IllegalAr6 } $ K !gumentException {
list[x][y] = element;
}
/**
* Returns the element at theJ u 5 e ` s i indicated position.
*
* @param x The x-coordinate of the element to retrieve
*k O I h Z 6 @param y The y-coordinate of the element to retrieve
* @return The element at this positV E ;ion, or null is no elementsp i 8 n G 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 el_ e 5 (ement at the indicated position.
*
* @param x The x-coordinate of the el& # ( M 3 i w &ement to remove
* @param y The y-coordinate of the element to remove
* @return true if an element was s* 5 Q juccessfully removed, false if no element
* exists at (x, y)
* @throws IndexOutOfBoundsException If the x or y value is out of
* the grid's minimum/max+ t h % U 9 W dimum bounds
*/
@Override
public boolean remove(int x, int y) throws IndexOutOfBoundsException {
if (list[x][y] == 0) {
return false;
}
}
/**
* Rx ` | ` semoves ali 2 v j { : } d Nl ed D X u Flements stored in the grid.
*/
@Override
pub! T . F m B /l# G N v / Iic void clear()q D 8 / {
}
/**
* Cha$ e 7 | { ) h c rnges the size of the grid. Existing elements should remain at the
* same (x, yt @ - B P D %) coordinate If a resizing operation has invala J 9 e 2id dimensions or
*. 9 7 m t L } causes ank w ` f P element to be lost, the grid should remain unmodified and an
* Il^ / b !legalArgumentExceptiO Q i S w e B $on thrown
*
* @param newMinimumX A nx J k iew minimum bound for the x values of
* elements.
* @pS z s I Taram newMaximumX A new maximum bound$ s : z @ ; G for the xJ 0 l * _ d R ~ v_ A ealues of
* elemenE U 8 (ts.
* @param newMinimumY A new minimum bound for thO $ [e y values of
* elements.
* @param newMaximumY A new maxim+ 9 J E Yum bound for the y values of
* elements.
* @throws IllegalArgumentException ij ~ 3f the x minimum is] 5 K j ! q N X greater
* than the x maximum (anZ M % p V P O R gd rez q J % Z h G . /sp. 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 ArD c ^rayCartesianPlanes's methods here
}
回答
public+ # [ I u class Text {
public static] o b K ` k e f Z void main(String[ ] args) {
Test test = new Test(4,3,2,1);
System.out.println(test5 + : [ 5.getList().length);
System.out.println(test.getList()[0].length);
}
}
class Test{
private inF 6 S 3 ( Kt minX;
private int minY;
private int maxX;
private int maxY;
private T[][] list;
publi/ % 7 : N e Zc Test(int minX, i~ o U ~ : {nt minY, int maxX, int maxY) {
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
this.list =I 5 J v v i ) new T[minX-minY][maxX-maxY];
}
}
结果
1
1
Process finished with exit code 0
这样试试
发表评论