【问题标题】:Reverse Java full document反向Java完整文档
【发布时间】:2014-05-11 05:48:58
【问题描述】:

我用了reverse一个字符串,但现在需要的是最终文件的原理,反之亦然:

Hello
Bye

Bye
hello

而不是:

olleH
eyB

当我这样做的时候?

这是我的来源:

public static void main(String[] args) {

    if (args.length != 1) {
        System.err.println("Sintaxis incorrecta, introduzca el nombre del fichero");
        System.exit(1);
    }

    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(args[0]));
        String s;

        try {
            while ((s = br.readLine()) != null) {
                StringBuilder reverse = new StringBuilder(s);
                String sCadenaInvertida = reverse.reverse().toString();
                System.out.println(sCadenaInvertida);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

谢谢!!

【问题讨论】:

  • 请改写but now need the final document is the principle, and vice versa:。这没有意义。
  • 分词,逆向集合,使用空格加入
  • 你需要获取String,使用split获取单词数组,然后新建一个String,按降序读取单词。简单:D
  • 在发布问题之前,请先查看预览部分,看看您的问题是否正确显示了您的问题。我稍微编辑了您的问题以显示您如何编写示例,但我不确定为什么有些大写变成小写,所以添加更多有关输入和预期输出的信息。
  • 现在看来您需要类似 FILO 的结构(如堆栈),或者只需将每一行添加到某个列表中,然后在完成添加 read it backward 之后。

标签: java string bufferedreader reverse stringbuilder


【解决方案1】:

将项目添加到数组中(先到先得),然后反向遍历数组

for (into I = array.length; i >= 0; i--) {
    //print array[i]
}

如果您不知道文档的行数,也可以使用 ArrayList

【讨论】:

    【解决方案2】:
    ArrayList<String> theWords= new ArrayList<String>();
    while ((s = br.readLine()) != null) {
                //split line into words
                String[] parts = s.split("\\s+"):
                //for each word append to arraylist
                for(String s : parts)
                {
                     theWords.append(s);
                } //end for loop
     } //end while loop
    
    // iterate array, from size-1 to 0
    int theWordsSize = theWords.size()--;
    for(int i= theWordsSize; i >= 0; i--)
    {
         System.out.println(theWords.get(i));
     } //end for loop
    

    【讨论】:

      【解决方案3】:

      只需将所有内容放入 ArrayList 并使用 Collections.reverse http://www.java2s.com/Code/Java/Collections-Data-Structure/ReverseorderofallelementsofJavaArrayList.htm

      伪代码:

          ArrayList<String> arrayList = new ArrayList<>();
          arrayList.add("Hello");
          arrayList.add("Bye");
      
          Collections.reverse(arrayList);
          System.out.println(arrayList);
      

      【讨论】:

      • 那只能用行来做,不能用文字来做
      • 同意,但它没有说任何关于颠倒的话。我认为他们只是希望输入与输入顺序相反。
      • 没错,需要按照斯科特所说的去做
      • 需要这个:你好,我的房子是白色的,再见,我的妈妈很糟糕再见,我的妈妈很糟糕你好,我的房子是白色的,与文件相反,而不是相反的单词。
      • @Dan 即使是文字,逻辑仍然适用于使用列表 + Collections.reverse
      【解决方案4】:

      答案在这里,很简单:

      公共类 Reverse2 {

      public static void main(String[] args) {
      
      
      
              if(args.length != 1){
                  System.err.println("Sintaxis incorrecta, introduzca el nombre del fichero");
                  System.exit(1);
              }
      
              BufferedReader br = null;
      
      
              try {
                  br = new BufferedReader(new FileReader(args[0]));
      
                  ArrayList<String> lista = new ArrayList<String>();
                  String s;
      
                  try {
                      while((s=br.readLine()) != null){
                          lista.add(s);
      
                      }
      
                      for(int i= lista.size()-1;i>=0;i--){
                          System.out.println(lista.get(i));
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
      
      
      
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }
      
      
      
      
      
          }
      
      }
      

      感谢所有可能的解决方案

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 2018-01-21
        • 2021-10-03
        • 2021-10-11
        • 2014-12-08
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多