import java.util.List;
import java.util.Arrays;// from 时 代 J a v a 公 众 号 - N o w J a v a . c o m
import java.util.Collections;
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
// create an ArrayList<String> from the contents of colors array
String[] colors = {"red", "white", "blue", "black", "yellow",
"purple", "tan", "pink"};
List<String> list = /**来自 N o w J a v a . c o m - 时代Java**/
new ArrayList<>(Arrays.asList(colors));
Collections.sort(list); // sort the ArrayList
System.out.printf("Sorted ArrayList: %s%n", list);
// search list for various values
printSearchResults(list, "black"); // first item
printSearchResults(list, "red"); // middle item
printSearchResults(list, "pink"); // last item
printSearchResults(list, "aqua"); // below lowest
printSearchResults(list, "gray"); // does not exist
printSearchResults(list, "teal"); // does not exist
}
// perform search and display result
private static void printSearchResults(
List<String> list, String key)
{
int result = 0;
System.out.printf("%nSearching for: %s%n", key);
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。