【问题标题】:Stringbuilder to 2D array JavaStringbuilder 到 2D 数组 Java
【发布时间】:2014-12-19 11:57:50
【问题描述】:

我正在做一个学校项目,但在将 Stringbuilder 转换为 2D 数组时遇到了一些麻烦。

stringbuilder sb 包含一个 2D 矩阵,我想将其转换为 2D int。

0 1 1 1 0 0 0 0 
1 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 
1 0 0 0 1 0 1 0
0 0 0 1 0 1 0 0 
0 0 0 0 1 0 0 0 
0 0 0 1 0 0 0 1 
0 0 0 0 0 0 1 0 

这是我要转换为 int[][] 的 sb 中的矩阵。

有什么想法吗?

【问题讨论】:

  • 您回答了自己的问题。学校项目....开始使用 urs 的那些灰色单元格
  • 如果我在这里问可能是我们必须通过搜索网络来做的事情,而不是在课堂上教。
  • 然后搜索网络,你在这里问一个问题。
  • 您需要thisthis。剩下的就看你自己了。

标签: java arrays matrix stringbuilder


【解决方案1】:

试试这个(解释在代码中)

public static void main(String[] args) {
    //assuming the stringbuilder has the contents below (each line ending in space followed by a newline character)
    final StringBuilder sb = new StringBuilder();
    sb.append("0 1 1 1 0 0 0 0 \n");
    sb.append("1 0 0 0 0 0 0 0 \n");
    sb.append("1 0 0 0 0 0 0 0 \n");
    sb.append("1 0 0 0 1 0 1 0 \n");
    sb.append("0 0 0 1 0 1 0 0 \n");
    sb.append("0 0 0 0 1 0 0 0 \n");
    sb.append("0 0 0 1 0 0 0 1 \n");
    sb.append("0 0 0 0 0 0 1 0 \n");

    //extract each line from the sb by splitting after the newline character \n
    final String[] rows = sb.toString().split("\n");
    //each line represent a row, so the numbers of lines is the number of rows in our matrix
    final int totalRows = rows.length;
    //the lenght of each line, if we replace space with nothing, represents the number of columns
    //columns have the same number of elements for each line so we can read it from the first row, row[0]. it will be the same for all the others 
    final int totalColumns = rows[0].replace(" ", "").length();
    //create matrix with proper size
    final int[][] matrix = new int[totalRows][totalColumns];

    //initialize first row index
    int currentRow = 0;
    //initialize first column index
    int currentColumn = 0;

    for (String row : rows) { //for each row
        row = row.substring(0, row.length() - 1); //remove last space character
        final String[] elements = row.split(" "); //store each element of the row in an array by splitting after the space character
        for (final String element : elements) { //for each element
            //add element in matrix at correct position
            matrix[currentRow][currentColumn] = Integer.parseInt(element); //convert string to int
            //increment column count for the next element
            currentColumn++;
        }
        //increment row count for the next row
        currentRow++;
        //reset column counter for the next row
        currentColumn = 0;
    }

    //display the matrix
    for (int i = 0; i < totalRows; i++) {
        for (int j = 0; j < totalColumns; j++) {
            if (j == 7) {
                //if last column add a newline after the element
                System.out.print(matrix[i][j] + "\r\n"); // where \r\n = CarriageReturn + LineFeed and is used as a new line character in Windows
                //or simply replace the above line with System.out.println(matrix[i][j]);
            } else {
                //if not last column add just a space after the element
                System.out.print(matrix[i][j] + " ");
            }
        }
    }
}

【讨论】:

  • \n\r\n 的有趣组合。你知道System.out.println() 方法存在吗?或使用this
  • @Tom 因为 op 正在处理 sb 中的换行符,所以我认为在显示矩阵时使用相同的矩阵会更清楚。由于这是一个学校项目,我怀疑他们的老师正在寻找一种单一的方法解决方案。
  • 这里没有描述\r\n的用法,但是ok。每个人都应该使用“Windows 风格”:D。
  • @Tom 好的,我编辑了它,你是对的,我应该解释一下
  • 我想改用println 会更容易,所以你不必处理不同的行尾类型。
猜你喜欢
  • 2023-03-23
  • 2021-07-06
  • 2023-03-20
  • 2012-03-18
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多