定义泛型数组,怎么创建一个已知大小的二维数组

图片说明

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
}


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

这样试试

this.list = new int[minX-minY][maxX-maxY];
改成
this.list = new T[minX-minY][maxX-maxY];

用的是泛型,类型是从创建对象时传入,不能创建成int的数组。如果确定是int就没必要用泛型了。