Import Java.util.collections;import Java.util.list;import java.text.dateformatsymbols;import Java.util.LinkedList; Public classsearchcollections { Public Static voidMain (string[] args) {List List=NewLinkedList (); /** * DateFormatSymbols is a public class used to encapsulate localizable date-time formatted data, such as month name, day of the week, and time zone data. DateFormat and SimpleDateFormat * Both use DateFormatSymbols to encapsulate this information. Sysmbols: Symbol, Tag * Constructs a DateFormatSymbols object by loading the format data from the default locale resource. */DateFormatSymbols DFS=NewDateFormatSymbols (); string[] months= Dfs.getmonths ();//Get the Month for(inti =0; i < months.length; i++) {String month=Months[i]; List.add (month); } collections.sort (list);//if it is not sorted here, the order is right, and the following results appear after sort!System. out. println ("name of month:"+list); /** * Month Name: [, January, July, March, September, February, May, August, June, November, December, October, April] * Find its location in: 10 * month = December */ intindex = Collections.binarysearch (list,"December");//Find the list ' December ' in a binary way if(index>0) {System. out. println ("find its location in:"+index); String Month= (String) list.Get(index);//List.get () method: Returns the element at the specified position in the list. System. out. println ("month ="+month); } }}
Search for collections elements, get month with DateFormatSymbols