如何创建内部类的对象数组?

时间:2014-08-04 10:37:33

标签: java android

我有一个名为table的类,其中有一个名为Cell的嵌套类。我将table划分为单元格。我想创建一个名为Cell的嵌套类的数组,我将自己引用到一些解释性教程,以了解如何从嵌套/内部类中实例化一个对象,但是,他们没有提供任何如何的示例实例化一个嵌套类的数组。以下贴出的是我的尝试和eclipse高亮cell = mTable.Cell[27];红色波浪

代码:Table_Class

// declaration
private Table mTable;
private Table.Cell []cell;
...
...
...
package com.example.kotschiena02;

public class Table {

    private int table_X1;
    private int table_Y1;
    private int table_X2;
    private int table_Y2;

    public Table(int x1, int y1, int x2, int y2) {
        this.table_X1 = x1;
        this.table_Y1 = y1;
        this.table_X2 = x2;
        this.table_Y2 = y2;
    }

    public int getTableWidth() {
        return (this.table_X2 - this.table_X1);
    }

    public int getTableHeight() {
        return (this.table_Y2 - this.table_Y1);
    }

    public int getTable_X1() {
        return this.table_X1;
    }

    public int getTable_Y1() {
        return this.table_Y1;
    }

    public int getTable_X2() {
        return this.table_X2;
    }

    public int getTable_Y2() {
        return this.table_Y2;
    }

    private class Cell {
        private int cell_ID;
        private int cell_X1;
        private int cell_Y1;
        private int cell_X2;
        private int cell_Y2;
        private boolean occupancyState;

        public Cell (int id, int x1, int y1, int x2, int y2, boolean occupancyState) {
            this.cell_ID = id;
            this.cell_X1 = x1;
            this.cell_Y1 = y1;
            this.cell_X2 = x2;
            this.cell_Y2 = y2;
            this.occupancyState = occupancyState;
        }

        public int getCell_X1() {
            return this.cell_X1;
        }

        public int getCell_Y1() {
            return this.cell_Y1;
        }

        public int getCell_X2() {
            return this.cell_X2;
        }

        public int getCell_Y2() {
            return this.cell_Y2;
        }

        public int getCell_ID() {
            return this.cell_ID;
        }

        public void setOccupancyState(boolean state) {
            this.occupancyState = state;
        }

        public boolean getOccupancyState() {
            return this.occupancyState;
        }
    }
}

代码:SetupTable()

private void setupTable() {
    // TODO Auto-generated method stub
    Log.i(TAG, "@setupTable:");

    mTable = new Table( 10, 
            ((screenHeight/2)-(2*cardHeight)), 
            (screenWidth-10), 
            ((screenHeight/2)+(2*cardHeight)) );

    cell = mTable.Cell[27];

1 个答案:

答案 0 :(得分:0)

public class Table {

//other properties
private List<Cell> cells;

class Cell {
//properties, getters and setters
}

//getters and setters
}

//Setting Up table
public void setupTable() {
// TODO Auto-generated method stub
Log.i(TAG, "@setupTable:");


Table mTable = new Table( 10, 
        ((screenHeight/2)-(2*cardHeight)), 
        (screenWidth-10), 
        ((screenHeight/2)+(2*cardHeight)) );

Table.Cell cell = mTable.getCells().get(27);
}