gpt4 book ai didi

java - 可编辑的二维数组

转载 作者:行者123 更新时间:2023-12-01 18:51:11 25 4
gpt4 key购买 nike

我想创建一个二维数组来创建飞机的迷你座位表。到目前为止,我已经成功打印出如下所示的内容:

1A(0) || 1B(0) || 1C(0)

2A(0) || 2B(0) || 2C(0)

3A(0) || 3B(0) || 3C(0)

4A(0) || 4B(0) || 4C(0)

零代表空座位,数字一代表有人座位。

我首先使用数组创建程序,这些数组是头等舱的类变量,但我想让该程序可用于经济舱部分。两个部分之间的唯一区别是数组的大小,因此我将代码编辑为如下所示:

public class Seating
{
private int FIRSTCLASS= 12;
private int ECONOMYCLASS= 240;
private int occupied, column;
private String[][] seatchart;
private int[][] seatlist;
private String[][] namelist;
private String name;
public String customer;

public Seating(String seatclass)
{
seatclass.toUpperCase();
if (seatclass.equals("FIRSTCLASS"))
{
seatchart= new String[FIRSTCLASS/3][3];
seatlist= new int[FIRSTCLASS/3][3];
namelist= new String[FIRSTCLASS/3][3];
}
else
if (seatclass.equals("ECONOMY"))
{
seatchart= new String[ECONOMYCLASS/3][3];
seatlist= new int[ECONOMYCLASS/3][3];
namelist= new String[ECONOMYCLASS/3][3];
}

}
public void Creation()
{
for (int i=0; i< seatlist.length; i++)
{
for (int j=0; j<seatlist[i].length; j++)
{
seatlist[i][j]= 0 ;

}
}

我在 for (int i=0; i< seatlist.length; i++) 周围收到空指针异常错误我该如何修复这个错误?

提前致谢!

最佳答案

问题出在这一行:

seatclass.toUpperCase();

替换为:

seatclass = seatclass.toUpperCase();

我认为您正在使用“firstclass”这样的字符串而不是“FIRSTCLASS”来创建类,对吧?这些不是相同的字符串,仅在字符串上调用 toUpperCase 方法而不将结果分配给要测试的变量意味着什么也不会发生。

然后,由于没有满足任何 if 条件,因此数组不会初始化,并且在调用 Completion() 时会引发空指针异常。

我不确定您是否是 Java 编程新手,但我想向您的类(class)添加一些建议:

public class Seating {

private static int FIRSTCLASS= 12; // Make these constants static since they pertain to all
private static int ECONOMYCLASS= 240; // instances of your class. That way there is exactly on
// copy of the variables, which is more memory efficient.
private int occupied;
private column; // Okay but Java convention is to declare each member variable on its own line
// increases code readability.
private String[][] seatchart;
private int[][] seatlist;
private String[][] namelist;
private String locSeatClass;
private String name;

public String customer; // Okay but better to leave this private and then provide getter and
// setter methods to provide access to this string. Much easier to track
// down who is changing its value in your code.

public Seating(String seatclass) { // Java convention is to place the opening bracket here not
// on the next line.
// Make sure that seatClass is not null or empty. NOTE: This is a neat trick for
// simultaneously checking for both null and empty strings in one shot. Otherwise, you have
// you have to check for null and then examine the string's length which is more code.
if ("".equalsIgnoreCase(seatClass) {
throw new IllegalArgumentException("Seat class undefined.");
}

// Store the seat class in a member variable for use. Could also be a local variable.
// My original solution is problematic because it changes the original value of the seat
// class that was passed into the constructor (you might want that information).
locSeatClass = seatclass.toUpperCase();

if (locSeatClass.equals("FIRSTCLASS"))
{
seatchart= new String[FIRSTCLASS/3][3];
seatlist= new int[FIRSTCLASS/3][3];
namelist= new String[FIRSTCLASS/3][3];
}
else if (locSeatclass.equals("ECONOMY")) {
seatchart= new String[ECONOMYCLASS/3][3];
seatlist= new int[ECONOMYCLASS/3][3];
namelist= new String[ECONOMYCLASS/3][3];
}
else {
// Throw an exception if someone passes in an unknown seat class string.
throw new IllegalArgumentException("Unknown seat class detected.")
}

}

public void creation() { // NOTE: Java convention is to begin method names with a lower
// case letter.

// This method is unnecessary. Arrays of integers are initialized with an initial value
// of zero by default. However, if you want to make your class reusable, you could change
// change the name of the this method to clear, which would allow you to clear the arrays of
// an existing object.
for (int i=0; i< seatlist.length; i++)
{
for (int j=0; j<seatlist[i].length; j++)
{
seatlist[i][j]= 0 ;

}
}

}

关于java - 可编辑的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15870575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com