繁体   English   中英

如何存储二维String数组(java)

[英]How to store 2-dimensional String array (java)

我正在尝试做一个二维String数组方法,该方法需要3个参数(String数组和两个整数),String数组已在main方法中定义为(String [] stringArray = {“1”,“2” ,“3”,“4”,“5”};)并且用户为行和列输入两个整数。 我希望代码能够多次运行,并且用户每次在用户输入的行和列的位置返回带有(“x”)的二维数组时输入行和列的值(“0 “) 除此以外。 我的问题是如何存储这个二维数组信息以供用户下次尝试。

//////////////////////////////////

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

首次尝试: - 用户输入:1表示行,1表示行。

Output:
1 x 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0

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

Output:
1 x 0 0
2 0 0 0
3 0 x 0
4 0 0 0
5 0 0 0

//////////////////////////////////

我的代码:

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;
}
}

我相信更容易使用字典或散列图。 您可以通过调用键而不处理数组索引来调用或更改字典中的值。

您可以将其存储在类字段中。 您可以使用字段String[][] array创建类SeatService ,并在main方法创建SeatService类的对象。 然后你可以在这个对象上调用方法seatInfo ,这个方法对array进行修改。

实施示例:

使用main方法的测试类:

public class Test {

    public static void main(String[] args) {
        SeatService seatService = new SeatService(4, 5);
        String[][] test1 = seatService.seatInfo(new String[] { "1", "2", "3", "4", "5" }, 2, 1);
        print(test1);
        String[][] test2 = seatService.seatInfo(new String[] { "1", "2", "3", "4", "5" }, 0, 0);
        print(test2);
    }

    public static void print(String[][] array) {
        System.out.print("Output: ");
        for (int i = 0; i < array.length; i++) {
            System.out.print(i + 1 + " ");
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
        }
        System.out.print("\n");
    }
}

SeatService类:

public class SeatService {
    int r;
    int c;
    String[][] array;

    public SeatService(int r, int c) {
        this.r = r;
        this.c = c;
        array = new String[r][c];
        cleanArray();
    }

    public String[][] seatInfo(String[] a, int rows, int lines) {
        if (rows < r && lines < c && rows >= 0 && lines >= 0)
            array[rows][lines] = "x";
        return array;
    }

    private void cleanArray() {
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                array[i][j] = "0";
            }
        }
    }
}

你应该分析几件事:

  1. 您的代码中的String[] a未使用
  2. 您对array操作将覆盖先前的修改
  3. 索引,数组中的第一个元素具有索引0,分析示例和您的代码,例如j < c - 1

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

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

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

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;
    }

}

但是,如果您只是希望函数处理新位置并返回更新的数组,那么我建议将函数设置为静态

public static String [][] seatInfo(String [][] a, int rows, int lines){
        String[][] returnString = a.clone();
        returnString[rows][lines] = "x";
        return returnString;
    }

然后在你的主要代码中

    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开始,以这种方式使用它们是最简单的,但是隐藏了用户的这些细节。 使用字符串作为方法参数时要小心,因为它们是不可变的。 字符串的更改不会反映在函数外部。 但是,您使用了一个字符串数组,它会稍微改变它。 在考虑如何保存数据时要小心。 String [] []实际上是一个3d数组。 在你的函数中你传递一个String [],它并没有真正帮助我们弄清楚如何处理我们的String [] [] seatingArray

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM