首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何存储二维字符串数组(java)

如何存储二维字符串数组(java)
EN

Stack Overflow用户
提问于 2019-03-20 05:00:19
回答 2查看 3.9K关注 0票数 0

我正在尝试做一个带有3个参数(字符串数组和两个整数)的二维字符串数组方法,字符串数组已经在main方法中定义为(String [] stringArray = {"1","2“,"3","4","5"};),用户输入行和列的两个整数。我希望代码多次运行,每次返回一个二维数组时,用户输入行和列的值,用户输入的行和列的位置为("x"),否则为("0")。我的问题是如何存储这个二维数组信息,以备用户下次尝试。

/

测试:(这个测试是我想要的,而不是代码做什么!)

第一次尝试:-The用户输入:1表示行,1表示行。

代码语言:javascript
复制
Output:
1 x 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0

第二次尝试:-The用户输入:行为2,行为3。

代码语言:javascript
复制
Output:
1 x 0 0
2 0 0 0
3 0 x 0
4 0 0 0
5 0 0 0

/

我的代码:

代码语言:javascript
复制
import java.util.Scanner;

public class Assignment03First{
public static void main(String [] args){

    Scanner in = new Scanner(System.in);
    Assignment03First test = new Assignment03First();

    String [] stringArray = {"1", "2" , "3", "4", "5"};

    test.intro();
    int input = 0;
    String result [][];
    do{
    System.out.println("If you would like to make a booking press 1, otherwise press 0");
    input = in.nextInt(); 

    if(input == 1){
        test.helper1(stringArray);
    }else{
        System.out.println("Take care. Can't wait to hear from you again!");
    }
    }while(input == 1);
}

public void intro(){

    System.out.println("Available seats:");
    System.out.println("  SEAT ROWS");
    System.out.println("   A  B  C");
    for(int y = 1; y <= 5; y++){
        System.out.println("-----------");
        System.out.print("|" + y);
        for(int z = 0; z < 3; z++){
            System.out.print("|" + 0 + "|");    
        }
        System.out.println();
    }
    System.out.println("-----------");
}

public int helper1(String [] a){
    Assignment03First test = new Assignment03First();
    Scanner in = new Scanner(System.in);

        int unkown = 0;

        System.out.println("Hello! Welcome to our online ticket booking application");
        System.out.println("Which seat row would you like to seat?");
        String r = in.next();
        if(r.equals("A")){
            unkown = 0;
        }else if(r.equals("B")){
            unkown = 1;
        }else{
            unkown = 2;
        }
        System.out.println("Which seat line would you like to seat?");
        int l = (in.nextInt() - 1);

        test.seatInfo(a, unkown, l);

    return l;
}

public String [][] seatInfo(String [] a, int rows, int lines){
    Assignment03First test = new Assignment03First();

    int r = 5;
    int c = 4;
    String[][] array = new String[r][c];

    for(int i= 0; i < r; i++){
        for(int j = 0; j < c - 1; j++){
            if(i == lines && j == rows){
                array [i][j] = "x";
            }else{ 
                array [i][j] = "0";
            }
        }
        test.helper2(array, r, c, a);
    }
    return array;
}

public String [][] helper2(String [][] a, int r, int c , String [] b){

    Assignment03First test = new Assignment03First();

    System.out.println("Available seats:");
    System.out.println("  SEAT ROWS");
    System.out.println("   A  B  C");

    for(int i= 0; i < r; i++){
        System.out.println("-----------");
        System.out.print("|" + b[i]);
        for(int j = 0; j < c - 1; j++){
        System.out.print("|" + a[i][j] + "|");
        }
        System.out.println();
        System.out.println("-----------");
    }
    return a;
}
}
EN

回答 2

Stack Overflow用户

发布于 2019-03-20 05:17:15

我相信使用字典或hashmap会更容易。只需调用键而不处理数组索引,就可以调用或更改字典中的值。

票数 0
EN

Stack Overflow用户

发布于 2019-03-20 07:24:19

我的第一个建议是尝试使用不同的数组索引表示法。在您的示例数组中,您声明用户将输入行,然后输入行。行通常是向上和向下的。我建议使用row,column或者i,j或者n,m这两种更常见的方式来描述2d数组中的位置。

此外,在本例中使用字符串数组会产生不必要的开销。一个二维布尔型数组就足够了。

要回答您的问题,这取决于您想要存储哪些座位被占用。如果您希望您的类存储其当前状态

代码语言:javascript
复制
public class SomeSeatClass {

    private String[][] currentState;

    public SomeSeatClass(int rows, int cols) {
        currentState = new String[rows][cols];
        // sets the size of the 2d array and then initialize it so that no seats are
        // taken.
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                currentState[i][j] = "0";
            }
        }
    }

    public void setSeat(int rows, int lines) {

        currentState[rows][lines] = "x";
    }

    public String[][] getCurrentState() {
        return this.currentState;
    }

}

但是,如果您只想让函数处理新位置并返回更新后的数组,那么我建议您将函数设为静态的,如下所示

代码语言:javascript
复制
public static String [][] seatInfo(String [][] a, int rows, int lines){
        String[][] returnString = a.clone();
        returnString[rows][lines] = "x";
        return returnString;
    }

然后在你的主代码中

代码语言:javascript
复制
    int numberOfRows =5;
    int numberOfColumns = 3;
    String[][] seating = new String[numberOfRows][numberOfColumns];
    //initialize array to "0"s
     for (int i = 0; i < numberOfRows; i++) {
            for (int j = 0; j < numberOfColumns; j++) {
                seating[i][j] = "0";
            }
        }
     //get the row and col from user
     int userRow = 1;
     int userCol = 1;
     //seating now holds the most current state 
    seating =  SomeSeatClass.seatInfo(seating, userRow, userCol);

我认为你遇到的问题是,你正在循环你的数组,并在每次函数调用时将所有东西重置为"0“。您只需要在开始时将所有内容设置为零,然后更新哪个座位被占用。

注意:要小心地认为数组是从1,1开始的。它们从0,0开始,这样使用起来最简单,但对用户隐藏了这些细节。使用字符串作为方法参数时要小心,因为它们是不可变的。对字符串的任何更改都不会反映在函数外部。但是,您使用了一个字符串数组,这对它进行了一些更改。在考虑如何保存数据时要小心。字符串实际上是一个3d数组。在您的函数中,您传递了一个String[],它并不能真正帮助我们弄清楚如何处理字符串seatingArray

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55249905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档