java - 使用数组列表按升序和降序排列字符串

标签 java sorting arraylist

我之前写过一篇文章,其中包含以下代码的更基本版本。

我重新整理了一下,还是不行。每当我输入一个新字符串时,它都不会进入两个列表中的任何一个。它给了我这个:

以下是按升序排列的字符串:[ ]

这是按降序排列的字符串:[]

公共(public)类字符串系列{

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
    String encore = scanner.nextLine(); 

    List<String> ascending = new ArrayList<>();
    List<String> descending = new ArrayList<>();

    int loop = 0;

    String longest = "";
    String lastInput = "";

    boolean inserted = false;

    while (!encore.equalsIgnoreCase("quit")) {

        loop = ++loop;

        encore = encore.replaceAll("\\s+",""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces.

        for(int i = 0; i < ascending.size(); i++) {
            if(ascending.get(i).length() > encore.length()) {
                ascending.add(i, encore);
                inserted = true;
            } if(!inserted) { 
            ascending.add(encore); }
        } for(int i = 0; i > descending.size(); i++) {              
            if(descending.get(i).length() < encore.length()) {
                descending.add(i, encore);
                inserted = true;
            } if(!inserted) { 
            descending.add(0, encore); }
                }

        if (longest.length() < encore.length()) {
            longest = encore; }

        System.out.println("Enter the string you want to put in your sequence of strings");

        encore = scanner.nextLine();
        }

    if (descending != null) { // we check to see if the "descending" string is empty (we could do this with "ascending" mind you).
        System.out.println("Here are your strings in ascending order : " + ascending);
        System.out.println("Here are your strings in descending order : " + descending);
        System.out.println("Here is the longest string : " + longest);
    } else if (descending == null) { 
        System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
        }
    }
}

最佳答案

我建议您使用 Collections.sort();Collections.reverse(); 对列表进行排序此外,您不需要 else if (descending == null) 因为您已经初始化了 descending。您的代码将类似于,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Test2 {
 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);
  System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
  String longest = "";

  List<String> ascending = new ArrayList<String>();
  List<String> descending = new ArrayList<String>();
  int loop = 0;
  Comparator<String> comparator = new Comparator<String>() {
   public int compare(String o1, String o2) {
    return o1.length() - o2.length();
   }
  }


  String encore = "";
  while(true){
   loop++;
   System.out.println("Enter the string you want to put in your sequence of strings");
   encore = scanner.nextLine();
   if (encore.equalsIgnoreCase("quit")) {
    break;
   }

   encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces

   ascending.add(encore);
   descending.add(encore);
   Collections.sort(ascending, comparator);
   Collections.sort(descending, comparator);
   Collections.reverse(descending);
  }

  for (String str: ascending) {
   if (str.length() > longest.length()) {
    longest = str;
   }
  }

  if (ascending.size() > 0) {
   System.out.println("Here are your strings in ascending order : " + ascending);
   System.out.println("Here are your strings in descending order : " + descending);
   System.out.println("Here is the longest string : " + longest);
  } else {
   System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
  }

  scanner.close();
 }
}

但是我只会使用一个列表而不是 2 个,因为它们都有相同的元素。就像,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Test2 {
 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);
  System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
  String longest = "";

  List < String > list = new ArrayList < > ();
  int loop = 0;

  String encore = "";
  while(true){
   loop++;
   System.out.println("Enter the string you want to put in your sequence of strings");
   encore = scanner.nextLine();
   encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces

   if (encore.equalsIgnoreCase("quit")) {
    break;
   }
   list.add(encore);
  }

  for (String str: list) {
   if (str.length() > longest.length()) {
    longest = str;
   }
  }

  if (list.size() > 0) {
   Collections.sort(list, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
     return o1.length() - o2.length();
    }
   });
   System.out.println("Here are your strings in ascending order : " + list);
   Collections.reverse(list);
   System.out.println("Here are your strings in descending order : " + list);
   System.out.println("Here is the longest string : " + longest);
  } else {
   System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
  }

  scanner.close();
 }
}

希望对你有帮助!

感谢 @phflack 指出排序应该按长度而不是按词汇顺序。

关于java - 使用数组列表按升序和降序排列字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46712587/

相关文章:

java - 将类转移到 ListActivity 上的其他类

java - CriteriaBuilder 使用自定义条件连接两个表

algorithm - 值循环的最小排序

PHP函数或算法来查找数组中的哪些元素加起来正好等于某个数字

Java:在两个ArrayList中查找匹配对象值的最有效方法?

java - 使用 Autowiring 字段的 Junit 测试

java - 无法写入android模拟器中的文件

algorithm - 选择排序顺序

Java ArrayList 避免 IndexOutOfBoundsException

java - 比较两个表中的值并将从第一个表中获取的值插入到第二个表中