我的Scanner为什么花费太长时间将输入转换为2D字符数组?

huangapple go评论62阅读模式
英文:

Why is my Scanner taking too long to convert inputs to a 2D char array?

问题

# Scanner to 2D char array taking too long time.
嗨!由于我不知道的某些原因,我的代码运行时间非常长,我唯一的猜测是Scanner太慢了,但我无法避免使用它,因为我需要它。seaCard的大小可以从1 * 1到10000 * 10000不等。CPU时间限制是8秒,这应该足够执行此操作。整个任务是在一个网格中计算岛屿的数量,但我甚至不能及时读取数据。

输入是这样的地图:
~@@~~
@~~~~.
```

我的代码如下:

```java
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int rows = input.nextInt();
        int cols = input.nextInt();
        input.nextLine();
        
        char[][] seaCard = new char[rows][cols];

        // making the map
        for(int i = 0; i < rows; i++){
            String thisRow = input.nextLine();
            for(int j = 0; j < cols; j++){
                seaCard[i][j] = thisRow.charAt(j);
            }
        }
        input.close();
```

我尝试过从2D数组更改为1D字符串数组,但效果不大。我还尝试过使用`next()`而不是`nextLine()`。

我期望代码能够顺利运行,对于`rows = 10000`和`cols = 10000`,输入为10000行长度为10000的字符串时,运行时间不超过6秒。

然而它根本无法完成,我唯一的想法是因为我使用了两个嵌套的循环,时间复杂度为n^2,但我觉得这也不应该导致运行时间如此之长。
```

<details>
<summary>英文:</summary>

# Scanner to 2D char array taking too long time. 
Hi! For some reason unkown to me makes my code take very long time, my only guess is that Scanner is slow however I cant escape it because I need to use it. The size of the seaCard can be anything from 1 * 1 to 10000 * 10000. The cpu time limit is 8 seconds which should be plenty of time to execute this. The whole assignment is to count islands in a grid but I cant even read the data in a timely manner. 

The inputs are maps like these 
```

~@@~~
@~~~~.


My code is like this.

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int rows = input.nextInt();
int cols = input.nextInt();
input.nextLine();

    char[][] seaCard = new char[rows][cols];

    // making the map
    for(int i = 0; i &lt; rows; i++){
        String thisRow = input.nextLine();
        for(int j = 0; j &lt; cols; j++){
            seaCard[i][j] = thisRow.charAt(j);
        }
    }
    input.close();

I have tried changing from 2D array to a 1D string array but that didnt change much, I have also tried using next() instead of nextLine(). 

I expected the code to run smoothly and not take longer than 6s for rows = 10000 and cols = 10000 with an string input of a 10000 string lines of length 10000. 

However it just dosent finish, my only idea is that because Im using 2 for loops I get a time complexity of n^2 but I feel like this still shouldnt make it take this long. 

</details>


# 答案1
**得分**: 0

这个版本从文件中获取输入。第一行应该是行数。无需从控制台输入。

```java
public static void main(String[] args) {
    try (Scanner fromFile = new Scanner(new File("C:/yourFilename.txt"))) {
        int rows = fromFile.nextInt(); // 从文件中读取行数。
        fromFile.nextLine();  // 从输入缓冲区中移除行尾符
        char[][] seaCard = new char[rows][];
        for (int r = 0; r < rows; r++) {
            seaCard[r] = fromFile.nextLine().toCharArray();
        }
        System.out.println("完成!");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
英文:

This version takes input from a file. The first line should be the number of rows. Nothing needs to be entered from the console.

public static void main(String[] args) {
    try (Scanner fromFile = new Scanner(new File(&quot;C:/yourFilename.txt&quot;))) {
        int rows = fromFile.nextInt(); // read the rows from the file.
        fromFile.nextLine();  // remove EOL from the input buffer
        char[][] seaCard = new char[rows][];
        for (int r = 0; r &lt; rows; r++) {
            seaCard[r] = fromFile.nextLine().toCharArray();
        }
        System.out.println(&quot;Done!&quot;);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}



</details>



huangapple
  • 本文由 发表于 2023年5月22日 07:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定