JAVAÀ༯¿ò¼ÜÖ÷Òª½Ó¿Ú
13.1.2 À༯¿ò¼ÜÖ÷Òª½Ó¿Ú
ÔÚÕû¸öJavaÀ༯ÖÐ×ʹÓõÄÀ༯½Ó¿ÚÊÇ£ºCollection¡¢List¡¢Set¡¢Map¡¢Iterator¡¢ListIterator¡¢Enumeration¡¢SortedSet¡¢SortedMap¡¢Queue¡¢Map.Entry£¬ÕâЩ½Ó¿ÚµÄ¾ßÌåÌØµãÈç±í13-1Ëùʾ¡£
±í13-1 À༯¿ò¼Ü½Ó¿ÚµÄ¾ßÌåÌØµã
Ðò ºÅ
½Ó ¿Ú
Ãè Êö
1
Collection
ÊÇ´æ·ÅÒ»×éµ¥ÖµµÄ×î´ó½Ó¿Ú£¬ËùνµÄµ¥ÖµÊÇÖ¸
¼¯ºÏÖеÄÿ¸öÔªËØ¶¼ÊÇÒ»¸ö¶ÔÏó¡£Ò»°ãºÜÉÙ
Ö±½ÓʹÓô˽ӿÚÖ±½Ó²Ù×÷
2
List
ÊÇCollection½Ó¿ÚµÄ×Ó½Ó¿Ú£¬Ò²ÊÇ×î³£ÓõĽӿڡ£
´Ë½Ó¿Ú¶ÔCollection½Ó¿Ú½øÐÐÁË´óÁ¿µÄÀ©³ä£¬
ÀïÃæµÄÄÚÈÝÊÇÔÊÐíÖØ¸´µÄ
3
Set
ÊÇCollection½Ó¿ÚµÄ×ÓÀ࣬ûÓжÔCollection
½Ó¿Ú½øÐÐÀ©³ä£¬ÀïÃæ²»ÔÊÐí´æ·ÅÖØ¸´ÄÚÈÝ
4
Map
ÊÇ´æ·ÅÒ»¶ÔÖµµÄ×î´ó½Ó¿Ú£¬¼´½Ó¿ÚÖеÄÿ¸ö
ÔªËØ¶¼ÊÇÒ»¶Ô£¬ÒÔkey→valueµÄÐÎʽ±£´æ
5
Iterator
¼¯ºÏµÄÊä³ö½Ó¿Ú£¬ÓÃÓÚÊä³ö¼¯ºÏÖеÄÄÚÈÝ£¬
Ö»ÄܽøÐдÓǰµ½ºóµÄµ¥ÏòÊä³ö
6
ListIterator
ÊÇIteratorµÄ×Ó½Ó¿Ú£¬¿ÉÒÔ½øÐÐË«ÏòÊä³ö
7
Enumeration
ÊÇ×îÔçµÄÊä³ö½Ó¿Ú£¬ÓÃÓÚÊä³öÖ¸¶¨¼¯ºÏÖеÄÄÚÈÝ
8
SortedSet
µ¥ÖµµÄÅÅÐò½Ó¿Ú£¬ÊµÏִ˽ӿڵļ¯ºÏÀ࣬
ÀïÃæµÄÄÚÈÝ¿ÉÒÔʹÓÃ±È½ÏÆ÷ÅÅÐò
9
SortedMap
´æ·ÅÒ»¶ÔÖµµÄÅÅÐò½Ó¿Ú£¬ÊµÏִ˽ӿڵļ¯ºÏÀ࣬
ÀïÃæµÄÄÚÈݰ´ÕÕkeyÅÅÐò£¬Ê¹ÓÃ±È½ÏÆ÷ÅÅÐò
10
Queue
¶ÓÁнӿڣ¬´Ë½Ó¿ÚµÄ×ÓÀà¿ÉÒÔʵÏÖ¶ÓÁвÙ×÷
11
Map.Entry
Map.EntryµÄÄÚ²¿½Ó¿Ú£¬Ã¿¸öMap.Entry¶ÔÏó
¶¼±£´æ×ÅÒ»¶Ôkey→valueµÄÄÚÈÝ£¬Ã¿¸öMap
½Ó¿ÚÖж¼±£´æÓжà¸öMap.Entry½Ó¿ÚʵÀý
ÕâЩ½Ó¿ÚÖб¾ÉíÊÇ´æÔڼ̳йØÏµµÄ£¬ÆäÖв¿·Ö½Ó¿ÚµÄ¼Ì³Ð¹ØÏµÈçͼ13-1Ëùʾ¡£
£¨µã»÷²é¿´´óͼ£©Í¼13-1 ½Ó¿ÚµÄ¼Ì³Ð¹ØÏµ
ÏÂÃæ½«½éÉÜÕâЩ½Ó¿ÚµÄ×÷Óü°Æä³£ÓÃ×ÓÀà¡£
UÌáʾ£ºSortedXx¶¨ÒåµÄ½Ó¿Ú¶¼ÊôÓÚÅÅÐò½Ó¿Ú¡£
ÔÚJavaÀ༯Öз²ÊÇÒÔSorted¿ªÍ·µÄÈ«²¿¶¼ÊôÓÚÅÅÐòµÄ½Ó¿Ú£¬ÈçSortedSet¡¢SortedMap¡£
13.2.1 Collection½Ó¿ÚµÄ¶¨Òå
Collection½Ó¿ÚµÄ¶¨ÒåÈçÏ£º
public interface Collection<E> extends Iterable<E>
´Ó½Ó¿ÚµÄ¶¨ÒåÖпÉÒÔ·¢ÏÖ£¬´Ë½Ó¿ÚʹÓÃÁË·ºÐ͵͍Ò壬ÔÚ²Ù×÷ʱ±ØÐëÖ¸¶¨¾ßÌåµÄ²Ù×÷ÀàÐÍ¡£ÕâÑù¿ÉÒÔ±£Ö¤À༯²Ù×÷µÄ°²È«ÐÔ£¬±ÜÃâ·¢ÉúClassCastExceptionÒì³£¡£
UÌáʾ£ºJDK 1.5Ö®ºóÀ༯²ÅÔö¼ÓÁË·ºÐ͵ÄÖ§³Ö¡£
ÔÚJDK 1.5֮ǰµÄÀ༯¿ò¼ÜÖпÉÒÔ´æ·ÅÈÎÒâµÄ¶ÔÏóµ½¼¯ºÏÖУ¬ÕâÑùÒ»À´ÔÚ²Ù×÷ʱ¾Í¿ÉÄܳöÏÖÒòΪÀàÐͲ»Í³Ò»¶øÔì³Éµ
Ïà¹ØÎĵµ£º
ComparatorºÍComparableÔÚÅÅÐòÖеÄÓ¦ÓÃ
µ±ÐèÒªÅÅÐòµÄ¼¯ºÏ»òÊý×é²»Êǵ¥´¿µÄÊý×ÖÐÍʱ£¬Í¨³£¿ÉÒÔʹÓÃComparator»òComparable£¬ÒÔ¼òµ¥µÄ·½Ê½ÊµÏÖ¶ÔÏóÅÅÐò»ò×Ô¶¨ÒåÅÅÐò¡£
Ò»¡¢Comparator
Ç¿ÐжÔij¸ö¶ÔÏócollection½øÐÐÕûÌåÅÅÐòµÄ±È½Ïº¯Êý£¬¿ÉÒÔ½«Comparator´«µÝ¸øCollections.sort»òArrays.sort¡£
½Ó¿Ú·½·¨£º
......
1¡¢ÈÕÆÚ¸ñʽ»¯
String tim = "2009-12-29";
String str = (new java.text.SimpleDateFormat("yyyyÄêMMÔÂddÈÕ")).format((new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(tim));
·µ»Ø£º2009Äê12ÔÂ29ÈÕ
String now = (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(new java.util.Date());
·µ»Øµ±Ç°Ê±¼ä ......
1¡¢javaÊÇ¿çÆ½Ì¨µÄ£¬ÊÇһÎâÊÍÐÔÓïÑÔ£¬Ò»ÌõÒ»ÌõµØÖ´ÐУ¬½âÊÍÆ÷¾ÍÊÇjvm£¬ËùÒÔ¿ÉÒÔ¿çÆ½Ì¨£»
C/C++ÊDZàÒëÐÔÓïÑÔ£¬ËùÓбàÒëÍêºóÔÙÖ´ÐУ¬¿çƽ̨µÄʱºòÐèÒªÖØÐ±àÒëÒ»´Î£¬ÒÔÊÊÓ¦ÐÂµÄÆ½Ì¨¡£
2¡¢ÏòÉÏתÐÍ£¬¼Ì³Ð °×Âí·ÇÂí
¸¸Àà¶ÔÏóת»¯Îª×ÓÀà±ØÐëÇ¿ÖÆ(SonClass)fatherClass
×ÓÀ൱ ......
µÚÒ»£¬Ì¸Ì¸final, finally, finalizeµÄÇø±ð¡£ ¡¡¡¡
final—ÐÞÊηû£¨¹Ø¼ü×Ö£©Èç¹ûÒ»¸öÀà±»ÉùÃ÷Ϊfinal£¬Òâζ×ÅËü²»ÄÜÔÙÅÉÉú³öеÄ×ÓÀ࣬²»ÄÜ×÷Ϊ¸¸Àà±»¼Ì³Ð¡£Òò´ËÒ»¸öÀ಻Äܼȱ»ÉùÃ÷Ϊ
abstractµÄ£¬ÓÖ±»ÉùÃ÷ΪfinalµÄ¡£½«±äÁ¿»ò·½·¨ÉùÃ÷Ϊfinal£¬¿ÉÒÔ±£Ö¤ËüÃÇÔÚʹÓÃÖв»±»¸Ä±ä¡£±»ÉùÃ÷ΪfinalµÄ±äÁ¿±ØÐëÔÚÉùÃ÷ʱ¸ø¶¨
......