使用数组列表按升序和降序排列字符串
问题描述:
我之前用一个更基本的以下代码版本编写了一篇文章。使用数组列表按升序和降序排列字符串
我重新安排了它,但它仍然不起作用。每当我输入一个新的字符串,它不会进入两个列表中的任何一个。它给了我这样的:
这是你的升序排列的字符串:[]
这是你的降序排列的字符串:[]
公共类Stringseries {
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();
对列表进行排序。此外,由于您已初始化descending
,因此您不需要else if (descending == null)
。您的代码看起来像,
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指出排序应该在长度上&不是词法顺序。
答
你的代码几乎是正确 为了实现插入排序,你只需要与移动你的if语句出你的循环,并重置inserted
变量
inserted = false;
for(int i = 0; i < ascending.size(); i++)
if(ascending.get(i).length() > encore.length())
{
ascending.add(i, encore);
inserted = true;
break;
}
if(!inserted)
ascending.add(encore);
inserted = false;
for(int i = 0; i > descending.size(); i++)
if(descending.get(i).length() < encore.length())
{
descending.add(i, encore);
inserted = true;
break;
}
if(!inserted)
descending.add(0, encore);
其他说明物联网您的代码:
-
loop = ++loop;
通常写为loop++;
代替 -
if(descending != null)
永远不会是假的,你用它在List<String> descending = new ArrayList<>();
顶部设置的东西,而不是它看起来像你的意思是写if(!descending.isEmpty())
- 写
if(descending != null){ A } else if(descending == null){ B }
相同(if descending != null){ A } else { B }
你不需要空当你用'new ArrayList'初始化时检查'降序' –
你怎么样不保存'降序',只是简单地按相反的顺序打印出集合? – cypher