之前我用以下代码的更基本版本撰写了一篇文章。
我重新安排了它,但它仍然不起作用。每当我输入一个新字符串时,它都不会进入两个列表中的任何一个。它给了我这个:
以下是按升序排列的字符串:[]
以下是您的字符串,按降序排列:[]
public class 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.
}
}
}
答案 0 :(得分:1)
我建议您使用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指出排序应该是长度&amp;不是词汇顺序。
答案 1 :(得分:1)
您的代码几乎是正确的
要实现插入排序,只需将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 }