【问题标题】:How to flip two words in a sentence in java如何在java中翻转句子中的两个单词
【发布时间】:2019-09-27 13:23:54
【问题描述】:

java中如何翻转句子中的两个单词

输入:“嗨,简今天过得怎么样”

输出:“你今天好吗,简”

我尝试了什么:

String s = "hi how are you doing today jane";
    ArrayList<String> al = new ArrayList<>();

    String[] splitted = s.split("\\s+");
    int n = splitted.length;
    for(int i=0; i<n; i++) {
        al.add(splitted[i]);
    }
  for(int i=0; i<n-1; i=i+2) {
    System.out.print(al.get(i+1)+" "+al.get(i)+" ");
  }
  if((n%2) != 0) {
          System.out.print(al.get(n - 1));
      }

我得到的输出: “你今天好吗”

【问题讨论】:

  • (1) 看看你的System.out.print 语句——你只输入了一个空格,而al.get(i) 之后确实需要第二个空格。 (2) 如果n 是奇数,您需要一种方法来解释最后一个元素(因为您编写的内容会成对打印它们)。这可以通过for 循环之后的简单if 语句来完成
  • 这个问题比上一个问题好多了。感谢您考虑人们的意见。 +1。
  • 你怎么知道要翻转哪两个词?
  • @JaneF 只需在循环末尾添加if((n % 2) != 0) System.out.print(a1.get(n - 1)); 即可翻译为“如果n 不是对,则打印最后一个元素”。
  • @PaulLemarchand 的回答更好。它和我做的一样,并使用更多的内置语言工具来做。我使用数组的替代方案不能与Collections.swap() 一起玩哦,而且他不使用备用数组(读者练习 - 更改我的代码以避免备用数组)。当然,当您使用ArrayList 和流时,实际创建的垃圾量要高得多。

标签: java arrays string arraylist


【解决方案1】:

正如您要求只使用一个循环并且没有大量使用正则表达式,这是使用Collections.swap 的另一种解决方案:

String s = "hi how are you doing today jane";
List<String> splitted = new ArrayList<>(List.of(s.split("\\s+")));

for(int i = 0; i < splitted.size() - 1; i += 2)
    Collections.swap(splitted, i, i + 1);
s = String.join(" ", splitted);
System.out.println(s);

输出:

你今天好吗,简

【讨论】:

  • 编辑:使用String.join() 而不是stream API,这似乎更具可读性和优化。
【解决方案2】:

由于您使用的是采用正则表达式的split(),因此使用正则表达式似乎是一种有效的解决方案,因此请使用它:

replaceAll("(\\w+)(\\W+)(\\w+)", "$3$2$1")

解释

(\\w+)   Match first word, and capture it as group 1
(\\W+)   Match the characters between the 2 words, and capture them as group 2
(\\w+)   Match second word, and capture it as group 3
$3$2$1   Replace the above with the 3 groups in reverse order

例子

System.out.println("hi how are you doing today jane".replaceAll("(\\w+)(\\W+)(\\w+)", "$3$2$1"));

输出

how hi you are today doing jane

注意:由于您的代码使用了split("\\s+"),因此您对“单词”的定义是一系列非空白字符。要使用该词的定义,请将正则表达式更改为:

replaceAll("(\\S+)(\\s+)(\\S+)", "$3$2$1")

【讨论】:

  • 我希望在逻辑上做到这一点,而不是通过正则表达式
  • "现在你有两个问题" :)
  • @Arkadiy 我确实喜欢这句话,但它确实不适用于这里,因为在这种情况下,正则表达式实际上是一个更简单的解决方案。 :)
【解决方案3】:

如果您想要老式的 fori 循环和 bufor/temp 值解决方案,您可以在这里:

public static void main(String[] args) {
    String s = "hi how are you doing today jane";
    String flip = flip(s);
    System.out.println(flip);
}

private static String flip(String sentence) {

    List<String> words = Arrays.asList(sentence.split("\\s+"));

    for (int i = 0; i < words.size(); i += 2) {
        if (i + 1 < words.size()) {
            String tmp = words.get(i + 1);
            words.set(i + 1, words.get(i));
            words.set(i, tmp);
        }
    }
    return words.stream().map(String::toString).collect(Collectors.joining(" "));
}

但是Pauls solultion 更好,因为它是 java,我们不再处于石器时代 :)

【讨论】:

  • 老式答案对每个人都很容易理解,包括学生和傻瓜
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多