如何使用值填充2D数组?

时间:2016-05-05 15:22:22

标签: java

所以我正在制定一个航空公司预订计划,我试图用每个航空​​公司的座位号码和变更填充二维阵列,这是我到目前为止所拥有的

public class BoeingSeatMap extends SeatMap{
protected int row;
protected char column;
SeatType seatType;
Seat s1;

public void intializeSeatMap(){
    this.row = 10;
    this.column =  7;
    char [][] seats = new char[row][column];
    for(int i = 0; i < seats.length; i++){
        for(int y = 0; y < seats[i].length; y++){
        //  seats[i][y] = s1.reserved(false);

        }
    }
}

}

这家航空公司有10行7列,前4行也是头等舱,布局是2-3-2(窗口,过道,过道,中间,过道,过道,窗口)

我想让阵列填充1A,1B etcetc。

2 个答案:

答案 0 :(得分:0)

你不能创建一个char数组来存储这样的数据,因为它只能存储一个字符('a','b','c'等)。如上所述,你必须选择另一种可以存储更多信息的数据类型,如String等。

或者更好的尝试制作自己的自定义对象,因为你已经有SeatType和Seat。

答案 1 :(得分:0)

您应该使用Seat[][]代替char[][],它可以包含您想要的所有数据。

这是一个基本的例子:

class Seat {

    private final String name;
    private final Type type;
    private boolean reserved = false;

    public Seat(String name, Type type) {
        this.name = name;
        this.type = type;
    }

    // ...Getters & Setters...

    public enum Type {
        Business,
        Economy;
    }
}

class BoeingSeatMap {
    private Seat[][] seats;

    public static final int rows = 10;
    public static final int collumns = 7;

    private static final String[] letters = { "A", "B", "C", "D", "E", "F", "G" };

    public void intializeSeatMap() {
        seats = new Seat[rows][collumns];

        for (int x = 0; x < seats.length; x++) {
            for (int y = 0; y < seats[x].length; y++) {
                String name = (x + 1) + letters[y];
                if(x < 4) { // first 4 rows are Business
                    seats[x][y] = new Seat(name, Seat.Type.Business);
                } else { // The rest Economy
                    seats[x][y] = new Seat(name, Seat.Type.Economy);
                }               
            }
        }
    }
}

示例输出:

[1A : Business, 1B : Business, 1C : Business, 1D : Business, 1E : Business, 1F : Business, 1G : Business]
[2A : Business, 2B : Business, 2C : Business, 2D : Business, 2E : Business, 2F : Business, 2G : Business]
[3A : Business, 3B : Business, 3C : Business, 3D : Business, 3E : Business, 3F : Business, 3G : Business]
[4A : Business, 4B : Business, 4C : Business, 4D : Business, 4E : Business, 4F : Business, 4G : Business]
[5A : Economy, 5B : Economy, 5C : Economy, 5D : Economy, 5E : Economy, 5F : Economy, 5G : Economy]
[6A : Economy, 6B : Economy, 6C : Economy, 6D : Economy, 6E : Economy, 6F : Economy, 6G : Economy]
[7A : Economy, 7B : Economy, 7C : Economy, 7D : Economy, 7E : Economy, 7F : Economy, 7G : Economy]
[8A : Economy, 8B : Economy, 8C : Economy, 8D : Economy, 8E : Economy, 8F : Economy, 8G : Economy]
[9A : Economy, 9B : Economy, 9C : Economy, 9D : Economy, 9E : Economy, 9F : Economy, 9G : Economy]
[10A : Economy, 10B : Economy, 10C : Economy, 10D : Economy, 10E : Economy, 10F : Economy, 10G : Economy]