gpt4 book ai didi

java - 按长度和字母顺序按行打印单词

转载 作者:行者123 更新时间:2023-12-01 18:49:59 25 4
gpt4 key购买 nike

我正在编写一个程序,其中数据文件具有多行单词,并且单词按其长度按行打印。例如,如果一个字的长度为 2,则数据文件中长度为 2 的所有字都将放在同一行上。这些单词需要按字母顺序排序(我假设我可以使用 arrays.sort)。

这是我的数据文件:

the jaguar is a wild cat species and only
extant member of genus panthera natives to americas present
range extends from southwestern united_states mexico in north_america
across much south paraguay northern argentina though
there are cats now living within_western has
largely_been extirpated since early_century it listed as near
threatened on red list its numbers declining_threats include loss
fragmentation habitat

我不知道如何实现按字长打印行。我目前制作了一个程序,将数据文件中的行转换为数组。


import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Bojing {
static String[] array;
public static void main(String args[]) throws IOException {
Scanner file = new Scanner(new File("bojing.dat"));
while(file.hasNext()) {
String line = file.nextLine();
Scanner chop=new Scanner(line);
while(chop.hasNext()) {
String word =chop.next();
}

String[] str = line.split("\\s+");
array = new String[str.length];
for (int i = 0; i < str.length; i++){
array[i] = (str[i]);
}

Arrays.sort(array);

System.out.println(Arrays.toString(array));

}
}
}

我当前的输出基于此代码:

[a, and, cat, is, jaguar, only, species, the, wild]
[americas, extant, genus, member, natives, of, panthera, present, to]
[extends, from, in, mexico, north_america, range, southwestern, united_states]
[across, argentina, much, northern, paraguay, south, though]
[are, cats, has, living, now, there, within_western]
[as, early_century, extirpated, it, largely_been, listed, near, since]
[declining_threats, include, its, list, loss, numbers, on, red, threatened]
[fragmentation, habitat]

这就是我的输出应该的样子。

a
as in is it of on to
and are cat has its now red the
cats from list loss much near only wild
genus range since south there
across extant jaguar listed living member mexico though
extends habitat include natives numbers present species
americas northern panthera paraguay
argentina
extirpated threatened
largely_been southwestern
early_century fragmentation north_america united_states
within_western
declining_threats

最佳答案

首先构建一个Map<Integer, List<String>> ,其中键是单词长度,列表包含该长度的单词,然后在打印时对每个列表进行排序。

如果您更改ListSet ,然后消除任何重复的单词。如果您随后使用 TreeMap对于Map ,以及 TreeSet对于Set ,结果将自动按长度排序,单词按字母顺序排序。

您可以使用 try-with-resources 和 Stream (Java 9+) 在 2 条语句中完成此操作:

try (Scanner file = new Scanner(input)) {
file.tokens()
.collect(Collectors.groupingBy(String::length, TreeMap::new,
Collectors.toCollection(TreeSet::new)))
.values().stream()
.map(list -> list.stream().collect(Collectors.joining(" ")))
.forEach(System.out::println);
}

输出

a
as in is it of on to
and are cat has its now red the
cats from list loss much near only wild
genus range since south there
across extant jaguar listed living member mexico though
extends habitat include natives numbers present species
americas northern panthera paraguay
argentina
extirpated threatened
largely_been southwestern
early_century fragmentation north_america united_states
within_western
declining_threats

关于java - 按长度和字母顺序按行打印单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59757215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com