如何在java中向字符串数组的内容添加更多内容
我正在试图找到一种方法来向已填充的数组中添加更多内容,程序的用户必须选择一个数组,例如seat[0][1],然后将他们的名字添加到他们选择的座位旁边。有没有办法做到这一点,或者有没有办法将他们选择的部分的内容更改为他们的名称?我使用的是2D字符串数组。这是我迄今为止编写的代码,如果您能提供任何建议,我将不胜感激如何在java中向字符串数组的内容添加更多内容,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我正在试图找到一种方法来向已填充的数组中添加更多内容,程序的用户必须选择一个数组,例如seat[0][1],然后将他们的名字添加到他们选择的座位旁边。有没有办法做到这一点,或者有没有办法将他们选择的部分的内容更改为他们的名称?我使用的是2D字符串数组。这是我迄今为止编写的代码,如果您能提供任何建议,我将不胜感激 {String [][] seat = new String[2][6]; seat[0][0] = "A.1"; seat[0][1] = "B.1"; seat[0][2] =
{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();}
数组的大小是固定的,因此,您应该使用
集合
,如数组列表
:
List<List<String>> arr = new ArrayList<>();
注意:
- 请记住,大多数编程语言中的索引都以
开头。因此,要访问第一个元素,必须使用索引0
0
集合
如数组列表
:
List<List<String>> arr = new ArrayList<>();
注意:
- 请记住,大多数编程语言中的索引都以
开头。因此,要访问第一个元素,必须使用索引0
0
集合
如数组列表
:
List<List<String>> arr = new ArrayList<>();
注意:
- 请记住,大多数编程语言中的索引都以
开头。因此,要访问第一个元素,必须使用索引0
0
集合
如数组列表
:
List<List<String>> arr = new ArrayList<>();
注意:
- 请记住,大多数编程语言中的索引都以
开头。因此,要访问第一个元素,必须使用索引0
0
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, ...)*/
}
然后将二维字符串数组更改为二维座椅数组
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");
.....
最后,您需要遍历数组的元素。然后,检查每个座位的名称是否存在,以及预订名称是否为“未知”。然后设置这个座位的预订名称
例如,程序的用户必须选择一个数组
座位[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, ...)*/
}
然后将二维字符串数组更改为二维座椅数组
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");
.....
最后,您需要遍历数组的元素。然后,检查每个座位的名称是否存在,以及预订名称是否为“未知”。然后设置这个座位的预订名称
例如,程序的用户必须选择一个数组
座位[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, ...)*/
}
然后将二维字符串数组更改为二维座椅数组
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");
.....
最后,您需要遍历数组的元素。然后,检查每个座位的名称是否存在,以及预订名称是否为“未知”。然后设置这个座位的预订名称
例如,程序的用户必须选择一个数组
座位[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, ...)*/
}
然后将二维字符串数组更改为二维座椅数组
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");
.....
最后,您需要遍历数组的元素。然后,检查每个座位的名称是否存在,以及预订名称是否为“未知”。然后设置这个座位的预订名称。作业上说,这是我最后使用的答案“飞机将有30排6列座位。行由数字1-30索引,列由字母a-F索引。在内部,您的程序应将座位表示为2D数组。当预订座位时,您的程序应存储与该座位相关联的名称。您可以将其实现为具有一个2D字符串数组,用于座位占用率和名称。” 例如,此答案将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();
}
}
}
字符串chosenseat,名称;
System.out.println(“座位地图:”);
字符串[][]座位=新字符串[2][3];
座位[0][0]=“A.1”;
席位[0][1]=“B.1”;
座位[0][2]=“C.1”;
座位[1][0]=“A.2”;
席位[1][1]=“B.2”;
席位[1][2]=“C.2”;
对于(inti=0;i这是我最后使用的答案,作业中说“飞机将有30行6列座椅。行由数字1-30索引,列由字母a-F索引。在内部,您的程序应将座椅表示为2D数组。当预订座椅时,您的程序应存储与该座椅相关的名称。您可以将此实现为具有单个2D字符串数组r座位占用率和姓名。”
例如,此答案将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();
}
}
}
字符串chosenseat,名称;
System.out.println(“座位地图:”);
字符串[][]座位=新字符串[2][3];
座位[0][0]=“A.1”;
席位[0][1]=“B.1”;
座位[0][2]=“C.1”;
座位[1][0]=“A.2”;
席位[1][1]=“B.2”;
席位[1][2]=“C.2”;
对于(inti=0;i这是我最后使用的答案,作业中说“飞机将有30行6列座椅。行由数字1-30索引,列由字母a-F索引。在内部,您的程序应将座椅表示为2D数组。当预订座椅时,您的程序应存储与该座椅相关的名称。您可以将此实现为具有单个2D字符串数组r座位占用率和姓名。”
例如,此答案将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();
}
}
}
字符串chosenseat,名称;
System.out.println(“座位地图:”);
字符串[][]座位=新字符串[2][3];
座位[0][0]=“A.1”;
座位[0