我试图找到一种方法来添加更多已经填充的数组,程序的用户必须选择一个数组,例如seat [0] [1],然后添加他们的名字应该添加在他们选择的座位旁边。有没有办法做到这一点,或者有没有办法改变他们选择的部分内容到他们的名字?我使用的是2D字符串数组。如果您能提供任何建议,我已经写过的代码,我将非常感谢。
{String [][] seat = new String[2][6];
seat[0][0] = "A.1";
seat[0][1] = "B.1";
seat[0][2] = "C.1";
seat[0][3] = "D.1";
seat[0][4] = "E.1";
seat[0][5] = "F.1";
seat[1][0] = "A.2";
seat[1][1] = "B.2";
seat[1][2] = "C.2";
seat[1][3] = "D.2";
seat[1][4] = "E.2";
seat[1][5] = "F.2";
//Print out array here using for-loops
System.out.println("Please choose your seat: ");
chosenseat=Keyboard.readString();
System.out.println("Please enter the name for the booking: ");
name=Keyboard.readString();}
答案 0 :(得分:2)
数组是固定大小的,所以你应该使用像Collection
这样的ArrayList
:
List<List<String>> arr = new ArrayList<>();
添加“行”:
arr.add(new ArrayList<>());
将一个元素添加到一行:
arr.get(0).add("..."); // add an element to the first row
arr.add(new ArrayList<>()); // add another row
arr.get(1).add("..."); // add an element to the second row
并获得一个元素:
// ...
arr.get(1).get(0); // get first element of the second row
注意:强>
0
开头。因此,要访问第一个元素,您必须使用索引0
。答案 1 :(得分:1)
程序的用户必须选择其中一个数组 座位[0] [1]然后添加他们的名字应该添加到座位旁边 他们选择了
我会创建一个类来处理:
class Seat {
private String name;
private String bookingName;
public Seat(String name, String bookingName){
this.name = name;
this.bookingName = bookingName;
}
public Seat(String name){
this(name, "unknown");
}
/*other stuff (getters, setters, ...)*/
}
然后将您的2D数组字符串更改为Seats的2D数组
Seat [][] seats = new Seat[2][6];
seats[0][0] = new Seat("A.1");
seats[0][1] = new Seat("B.1");
seats[0][2] = new Seat("C.1");
.....
最后,您需要循环遍历数组的元素。然后,对于每个座位,检查它的名称是否存在以及bookingName不是“未知”。然后设置此座位的预订名称。
答案 2 :(得分:0)
这是我最终使用的答案,作业说“飞机将有30行和6列座位。行数用1-30索引,列用字母AF编制索引。内部你的程序应该将座位表示为2D阵列。当预订座位时,您的程序应该存储与该座位相关联的名称。您可以将其实现为具有单个2D阵列的字符串,用于座位占用和名称。 这个答案用例如A.1替换A.1。
String chosenseat, name;
System.out.println("The seat map: ");
String [][] seat = new String[2][3];
seat[0][0] = "A.1";
seat[0][1] = "B.1";
seat[0][2] = "C.1";
seat[1][0] = "A.2";
seat[1][1] = "B.2";
seat[1][2] = "C.2";
for(int i=0;i<seat.length;i++)//loop through the rows
{
for(int j=0;j<seat[0].length;j++)//loop through the columns
{
System.out.print(seat[i][j]+" ");
}
System.out.println();
}
String stringToSearch;
System.out.println("What seat would you like:");
stringToSearch=Keyboard.readString();
System.out.println("Please enter your name: ");
name=Keyboard.readString();
for (int i = 0; i <seat.length; i++)
{
for (int j = 0; j < seat[0].length; j++)
{
if (seat[i][j].equals(stringToSearch))
System.out.print(i);
}
}
for (int i = 0; i <seat.length; i++)
{
for (int j = 0; j < seat[0].length; j++)
{
if (seat[i][j].equals(stringToSearch))
System.out.println("."+j);
}
}
for(int i=0;i<seat.length;i++)//loop through the rows
{
for(int j=0;j<seat[0].length;j++)//loop through the columns
{
if (seat[i][j].equals(stringToSearch))
seat[i][j]=name;
}
}
for(int i=0;i<seat.length;i++)//loop through the rows
{
for(int j=0;j<seat[0].length;j++)//loop through the columns
{
System.out.print(seat[i][j]+" ");
}
System.out.println();
}
}
}