【问题标题】:how to reverse the word using list iterator in java如何在java中使用列表迭代器来反转单词
【发布时间】:2015-12-15 07:03:04
【问题描述】:
class StringRev{
    public static void main(String args[]){
    String str = "He is the one";
    String temp = "";
    String finalString = "";
        for(int i =str.length()-1;i>=0;i--){
            temp +=i!=0?str.charAt(i):str.charAt(i)+" ";
            if(str.charAt(i) == ' '||i==0){
                for(int j=temp.length()-1;j>=0;j--){
                    finalString += temp.charAt(j);
                }
                temp = "";
            }
        }
            System.out.println(finalString);
    }
}

我试图解决。我必须将字符串 "He is the one" 反转为 "one the is He"
我已经用 Java 编写了一些程序,但正在寻找其他最佳解决方案。
建议任何可能的方法来最小化电流。

【问题讨论】:

  • 只需使用空格分割字符串,会给你一个单词数组,反向迭代并将其附加到新字符串。

标签: java string


【解决方案1】:

将字符串拆分为单词并向下迭代并将单词附加回字符串。

public class StringRev{
    public static void main(String args[]){
        String input = "He is the one";
        String output = "";
        String[] inputSplit = input.split(" ");

        for(int i=inputSplit.length-1; i>=0; i--){
            output += inputSplit[i] + " ";
        }

        System.out.println(output.trim()); //remove trailing space
    }
}

如果您可以使用CollectionsStringUtil,那就更直接了。

public class StringRev{
    public static void main(String args[]){
        String input = "He is the one";
        String[] inputSplit = input.split(" ");

        Collections.reverse(Arrays.asList(inputSplit));
        String output = StringUtil.join(Arrays.asList(inputSplit), " ");

        System.out.println(output);
    }
}

【讨论】:

  • 要使用 StringUtil 或 StringUtils 类,我们需要使用额外的库。见:stackoverflow.com/questions/8660151/…
  • @ParkashKumar 这很明显不是吗?当您想使用集合时,您需要java.util.Collections,而我不会在您的答案中尝试它。停止试图让其他人的答案看起来不如你的好,只是为了得到那几点..
  • 这不是重点。它关于简单明了的答案,满足了OP的要求。虽然,要使用 java.util.Collections,除了 JDK / JRE 之外,您不需要任何其他库,显然您将使用您的 java 程序来运行。
  • "如果你可以使用CollectionsStringUtil"
  • 我真的不是故意降低你的答案。抱歉,如果您介意我最初的评论。
【解决方案2】:

只需split 给定空格字符串,就会给你一个单词数组,反向迭代并将其附加到新字符串,如下所示:

String string = "He is the one";
System.out.println("Original String: " + string);

String[] arr = string.split(" ");

String reverseString = "";
for(int i = arr.length - 1; i >= 0; i--){
    reverseString += arr[i] + " ";
}

System.out.println("Reverse String: " + reverseString);

或者您可以使用Arrays 实用程序将拆分后的字符串转换为List,使用Collection 实用程序reverse 方法将其反转,对其进行迭代并创建字符串如下:

List<String> list = Arrays.asList(string.split(" "));
Collections.reverse(list);

String reserveStringUsingList = "";
for(String str : list){
    reserveStringUsingList += str + " ";
}

System.out.println("Reverse String using List: " + reserveStringUsingList);

【讨论】:

  • 现在你甚至试图从我的答案中窃取内容并呈现为你自己的东西?可悲。
  • 我们都在学习 JAVA。我刚刚改进了我的答案。同时,无需添加有关 trim() 的详细信息。
  • 是的,您在阅读我的回答后改进了它。没有人从 Java 中窃取,看起来你不明白这个词的含义。我通常不介意人们这样做,但如果你偷窃,同时试图让我的回答看起来像垃圾,那么我不会默默地让它溜走,因为这种态度真的不受欢迎所以。
  • 好的,我已经更新了我的答案。不要自大,也许你更懂文字游戏。
【解决方案3】:

这是您要求的解决方案,使用ListIterator

代码:

public static void main(String[] args) {
    final String str = "He is the one";
    System.out.println(str);
    System.out.println(reverseWords(str));
}

static String reverseWords(String str) {
    StringBuilder result = new StringBuilder(str.length());
    List<String> words = Arrays.asList(str.split(" "));
    if(!words.isEmpty()) {
        ListIterator<String> it = words.listIterator(words.size());
        result.append(it.previous());
        while (it.hasPrevious()) {
            result.append(' ').append(it.previous());
        }
    }
    return result.toString();
}

输出:

He is the one
one the is He

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2019-01-20
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多