简体   繁体   English

如何在Java中删除2D数组中的空格?

[英]How to delete spaces In an 2D-Array in Java?

I have a text file. 我有一个文本文件。 I am reading it then placing it into a 2D-Array. 我正在阅读,然后将其放入2D阵列。 There are spaces. 有空格。 I need to get rid of those spaces. 我需要摆脱那些空间。 But I can't use trim properly. 但是我不能正确使用trim Here is my code: 这是我的代码:

while ((line = br.readLine() ) != null ){

    char[] row = line.toCharArray();
    line.trim();
    int counter = 0;
    for (int i = 0; i < row.length; i++) {
        maze[counter][i] = row[i];
        System.out.print(maze[i]);
        counter++;
    }
    System.out.printf("%n");
}

The output is as follows: 输出如下:

1                    1                    1                    0
0                    0                    1                    0
0                    0                    1                    0
0                    9                    1                    0

The elements in the text file I read has one space between each other. 我阅读的文本文件中的元素之间有一个空格。 But I get too many spaces as output. 但是我得到的输出太多了。 I need to get this as 我需要得到这个

1110
0010
0010
0910

I think I should use trim method, but I could not figure it out. 我认为我应该使用trim方法,但我无法弄清楚。

You can use String#split with a regular expression of something like \\s+ , for example... 例如,您可以将String#split\\s+类的正则表达式结合使用...

    String text = "1                    1                    1                    0";
    String elements[] = text.split("\\s+");
    for (String value : elements) {
        System.out.println("[" + value + "]");
    }

Which outputs 哪个输出

[1]
[1]
[1]
[0]

(The braces are there to demonstrate that no spaces remain) (括号显示那里没有空格)

In your example I might still be tempted to still us line = line.trim(); 在您的示例中,我可能仍然倾向于使用line = line.trim(); to ensure that there are no leading or trailing space which might cause empty values to be included... 以确保没有前导或尾随空格可能会导致包含空值的情况...

You can use (string).replace(" ", '\\0') to replace all spaces with blanks 您可以使用(string).replace(" ", '\\0')将所有空格替换为空格

For example: 例如:

    String line = "1 2  2 3 4 2 122 23  3  3 3 3";   //example
    line = line.replace(' ', '\0');  //'\0' is the key for blank (or nothing)
    System.out.println(line);

will produce 将产生

122342122233333

This will get rid of the spaces and only use the valid input (ie the numbers). 这将消除空格,仅使用有效输入(即数字)。 It says row, but the only input will be the same characters. 它说行,但是唯一的输入将是相同的字符。

Hope this helps. 希望这可以帮助。

Quickest way for me would be to just use a nested loop to print each element of the array individually. 对我而言,最快的方法是只使用嵌套循环分别打印数组的每个元素。 Eg 例如

String [][] maze = new String [4][4];

     for (int i = 0; i < maze.length; i++) {

         maze[i][0] = "1";
         maze[i][1] = "0";
         maze[i][2] = "1";
         maze[i][3] = "0";

     }

     for (int k =0;k<maze.length;++k){

         for(int j=0;j<maze.length;++j)
         {
             System.out.print(maze[k][j]);
         }
         System.out.println();
     }

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

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