Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,3 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
void printCompare(String stringA, String stringB) {
|
||||
if (stringA.length() > stringB.length()) {
|
||||
System.out.printf("%d %s%n", stringA.length(), stringA);
|
||||
System.out.printf("%d %s%n", stringB.length(), stringB);
|
||||
} else {
|
||||
System.out.printf("%d %s%n", stringB.length(), stringB);
|
||||
System.out.printf("%d %s%n", stringA.length(), stringA);
|
||||
}
|
||||
}
|
||||
|
||||
void printDescending(String... strings) {
|
||||
List<String> list = new ArrayList<>(List.of(strings));
|
||||
list.sort(Comparator.comparingInt(String::length).reversed());
|
||||
for (String string : list)
|
||||
System.out.printf("%d %s%n", string.length(), string);
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package stringlensort;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class ReportStringLengths {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] list = {"abcd", "123456789", "abcdef", "1234567"};
|
||||
String[] strings = args.length > 0 ? args : list;
|
||||
|
||||
compareAndReportStringsLength(strings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare and report strings length to System.out.
|
||||
*
|
||||
* @param strings an array of strings
|
||||
*/
|
||||
public static void compareAndReportStringsLength(String[] strings) {
|
||||
compareAndReportStringsLength(strings, System.out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare and report strings length.
|
||||
*
|
||||
* @param strings an array of strings
|
||||
* @param stream the output stream to write results
|
||||
*/
|
||||
public static void compareAndReportStringsLength(String[] strings, PrintStream stream) {
|
||||
if (strings.length > 0) {
|
||||
strings = strings.clone();
|
||||
final String QUOTE = "\"";
|
||||
Arrays.sort(strings, Comparator.comparing(String::length));
|
||||
int min = strings[0].length();
|
||||
int max = strings[strings.length - 1].length();
|
||||
for (int i = strings.length - 1; i >= 0; i--) {
|
||||
int length = strings[i].length();
|
||||
String predicate;
|
||||
if (length == max) {
|
||||
predicate = "is the longest string";
|
||||
} else if (length == min) {
|
||||
predicate = "is the shortest string";
|
||||
} else {
|
||||
predicate = "is neither the longest nor the shortest string";
|
||||
}
|
||||
//@todo: StringBuilder may be faster
|
||||
stream.println(QUOTE + strings[i] + QUOTE + " has length " + length
|
||||
+ " and " + predicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue