英文:
How can I store more than one word inside a txt file into a String in one sitting?
问题
所以我有一个名为`dictionary.txt`的txt文件,里面包含了一个非常长的单词列表。每个单词占据一行,格式如下:
priceably
southwesterly
semipapal
genuine
cardiotoxicities
shoeman
Utahan
ungeodetically
Schurman
...
我仍然在努力寻找一种方法,将它们全部存储在一个字符串数组中,而不必一个一个地操作。
到目前为止,这是我得到的代码:
```java
String[] words = new String[0];
int i = 0;
try
{
Scanner scnr = new Scanner(new File("dictionary.txt"));
String line;
while (scnr.hasNext())
{
line = scnr.nextLine();
String[] lineWords = line.split("\r?\n|\r");
words = concatenate(words, lineWords);
}
scnr.close();
}
如何优化这段代码,以便程序能更快地完成这个过程?
<details>
<summary>英文:</summary>
So I have a really long list of words inside a txt called `dictionary.txt`. It has one word, then it skips to a new line and has the next one as follows:
priceably
southwesterly
semipapal
genuine
cardiotoxicities
shoeman
Utahan
ungeodetically
Schurman
...
I am still struggling to find a way to store them all in an array of Strings without having to do it one by one.
This is what I got so far:
```java
String[] words = new String[0];
int i = 0;
try
{
Scanner scnr = new Scanner(new File("dictionary.txt"));
String line;
while (scnr.hasNext())
{
line = scnr.nextLine();
String[] lineWords = line.split("\r?\n|\r");
words = concatenate(words, lineWords);
}
scnr.close();
}
How can I optimize this better so that the program finishes this process faster?
答案1
得分: 0
以下是您要求的部分内容的翻译:
如果将来有人用得上的话,这是我在评论中实现解决方案的方式(我没有继续使用数组列表,因为我几乎不知道如何使用它们,而且截止日期正在逼近,根本来不及学习它们):
```java
public static void main(String[] args) throws IOException {
NumberFormat formatter = NumberFormat.getNumberInstance(Locale.US); // 格式化显示的纳秒数
List<String> words = Files.readAllLines(Paths.get("dictionary.txt"), StandardCharsets.UTF_8); // 读取文本文件并将其存储在数组列表中
String[] transformed = words.toArray(new String[words.size()]); // 将数组列表转换为字符串数组...
String[] wordsToSort = transformed.clone(); // 复制以便将来安全更改它...
// 测试排序算法...
这行代码 List<String> words = Files.readAllLines(Paths.get("dictionary.txt"), StandardCharsets.UTF_8);
一次性将超过 600,000 个元素存储在数组列表中。这样,您不需要循环一个个地存储它们。
<details>
<summary>英文:</summary>
In case this is useful for anyone in the future. This is how I implemented the solution in the comments (I didn't keep using array lists because I barely know how to use them and the deadline was encroaching forward to even start learning that now):
```java
public static void main(String[] args) throws IOException {
NumberFormat formatter = NumberFormat.getNumberInstance(Locale.US); // formats the amount of nanoseconds displayed
List<String> words = Files.readAllLines(Paths.get("dictionary.txt"), StandardCharsets.UTF_8); // reads txt file and stores it in an array list
String[] transformed = words.toArray(new String[words.size()]); // transforms array list into a String array...
String[] wordsToSort = transformed.clone(); // makes a copy to safely change it in the future...
// Testing Sorting algorithms......
This line List<String> words = Files.readAllLines(Paths.get("dictionary.txt"), StandardCharsets.UTF_8);
stores more than 600000 elements into an array list in one sitting. In this way, you do not need a loop to store them all one-by-one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论