lllrc 2020-08-17 15:39 采纳率: 0%
浏览 285
已采纳

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

图片说明

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
}


  • 写回答

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
    

    这样试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘