Java 为什么我的阵列没有被对象填充/显示?
所以基本上我想用对象填充一个数组,然后显示它。(稍后,这些对象将能够作为“已预订”显示,这将使它们在阵列中显示为X。) 但是,每当我执行生成和显示数组的方法时,总是会出现错误 这是我的密码: 主要内容: 创建/填充数组的席位类方法:Java 为什么我的阵列没有被对象填充/显示?,java,arrays,methods,2d,Java,Arrays,Methods,2d,所以基本上我想用对象填充一个数组,然后显示它。(稍后,这些对象将能够作为“已预订”显示,这将使它们在阵列中显示为X。) 但是,每当我执行生成和显示数组的方法时,总是会出现错误 这是我的密码: 主要内容: 创建/填充数组的席位类方法: public void SetType(String x) { type = x; } public boolean SetStatus() { return status; } publ
public void SetType(String x) {
type = x;
}
public boolean SetStatus() {
return status;
}
public void GetStatus(boolean x) {
status = x;
}
public String toString() {
String Seat = type;
return type;
}
public String BookSeat() {
type = "x";
return type;
}
public Seat[][] CreateTheatre(Seat[][] x) {
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 8; col++) {
x[row][col] = new Seat("B");
}
for (int col = 8; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 19; row < 23; row++) {
for (int col = 0; col < 8; col++) {
x[row][col] = new Seat("B");
}
for (int col = 8; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 4; row < 9; row++) {
for (int col = 3; col < 5; col++) {
x[row][col] = new Seat("B");
}
for (int col = 5; col < 9; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 4; row < 7; row++) {
for (int col = 9; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 14; row < 20; row++) {
for (int col = 3; col < 5; col++) {
x[row][col] = new Seat("B");
}
for (int col = 5; col < 9; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 16; row < 20; row++) {
for (int col = 9; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 9; row < 14; row++) {
for (int col = 6; col < 9; col++) {
x[row][col] = new Seat("G");
}
}
for (int row = 7; row < 16; row++) {
for (int col = 9; col < 12; col++) {
x[row][col] = new Seat("G");
}
}
return x;
}
public void DisplayTheatre(Seat[][] x) {
for (int row = 0; row < x.length; row++) {
for (int col = 0; col < x[row].length; col++) {
System.out.print(x[row][col]);
}
System.out.println();
}
}
}
public void SetType(字符串x){
类型=x;
}
公共布尔集合状态(){
返回状态;
}
public void GetStatus(布尔x){
状态=x;
}
公共字符串toString(){
弦座=类型;
返回类型;
}
公共字符串BookSeat(){
type=“x”;
返回类型;
}
公共座位[][]创意剧场(座位[][]x){
用于(int行=0;行<4;行++){
for(int col=0;col<8;col++){
x[世界其他地区][col]=新座位(“B”);
}
for(int col=8;col<12;col++){
x[世界其他地区][col]=新座位;
}
}
对于(int行=19;行<23;行++){
for(int col=0;col<8;col++){
x[世界其他地区][col]=新座位(“B”);
}
for(int col=8;col<12;col++){
x[世界其他地区][col]=新座位;
}
}
for(int行=4;行<9;行++){
for(int col=3;col<5;col++){
x[世界其他地区][col]=新座位(“B”);
}
for(int col=5;col<9;col++){
x[世界其他地区][col]=新座位;
}
}
对于(int行=4;行<7;行++){
for(int col=9;col<12;col++){
x[世界其他地区][col]=新座位;
}
}
用于(int行=14;行<20;行++){
for(int col=3;col<5;col++){
x[世界其他地区][col]=新座位(“B”);
}
for(int col=5;col<9;col++){
x[世界其他地区][col]=新座位;
}
}
用于(int行=16;行<20;行++){
for(int col=9;col<12;col++){
x[世界其他地区][col]=新座位;
}
}
用于(int行=9;行<14;行++){
for(int col=6;col<9;col++){
x[行][列]=新座位(“G”);
}
}
用于(int行=7;行<16;行++){
for(int col=9;col<12;col++){
x[行][列]=新座位(“G”);
}
}
返回x;
}
公共剧场(座位[][]x){
对于(int row=0;row
在CreateTheatre(…)
中,首先必须确保数组实际上与在for
循环中处理的数组一样大,如果数组较小,尝试访问它们会抛出ArrayIndexOutOfBoundsException
以生成随机索引:new java.util.random().nextInt()不要在这里发布整个项目,只需发布代码的相关部分以及stacktrace或错误消息。这应该足以帮助我们找出问题所在。啊,好吧,那么for(int row=0;row<4;row++){for(int col=0;col<8;col++){x[行][col]=新座位(“B”);
没有用新的seat对象填充数组?@user1719605我没有从整体上检查您的代码,但乍一看我就知道这是问题所在,可能是也可能不是…这是我运行代码时收到的错误消息,并键入将执行方法的输入。如果这有帮助,请在“线程”main中键入.Exception“java.lang.ArrayIndexOutOfBoundsException:19位于cinemasystem.Seat.CreateTheatre(Seat.java:65)位于cinemasystem.cinemasystem.main(cinemasystem.java:89)java结果:1“好的,我这样做了,它现在正在加载数组中的值并显示它们。但是我现在遇到了显示null的问题,在我不想显示任何内容的坐标中,有没有办法让它显示空白而不是“null”?@user1719605在显示它之前,检查如果(x[row][col]!=null)
。
public void SetType(String x) {
type = x;
}
public boolean SetStatus() {
return status;
}
public void GetStatus(boolean x) {
status = x;
}
public String toString() {
String Seat = type;
return type;
}
public String BookSeat() {
type = "x";
return type;
}
public Seat[][] CreateTheatre(Seat[][] x) {
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 8; col++) {
x[row][col] = new Seat("B");
}
for (int col = 8; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 19; row < 23; row++) {
for (int col = 0; col < 8; col++) {
x[row][col] = new Seat("B");
}
for (int col = 8; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 4; row < 9; row++) {
for (int col = 3; col < 5; col++) {
x[row][col] = new Seat("B");
}
for (int col = 5; col < 9; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 4; row < 7; row++) {
for (int col = 9; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 14; row < 20; row++) {
for (int col = 3; col < 5; col++) {
x[row][col] = new Seat("B");
}
for (int col = 5; col < 9; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 16; row < 20; row++) {
for (int col = 9; col < 12; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 9; row < 14; row++) {
for (int col = 6; col < 9; col++) {
x[row][col] = new Seat("G");
}
}
for (int row = 7; row < 16; row++) {
for (int col = 9; col < 12; col++) {
x[row][col] = new Seat("G");
}
}
return x;
}
public void DisplayTheatre(Seat[][] x) {
for (int row = 0; row < x.length; row++) {
for (int col = 0; col < x[row].length; col++) {
System.out.print(x[row][col]);
}
System.out.println();
}
}
}