ºìÁªLinuxÃÅ»§
Linux°ïÖú

³õѧÕ߱ضÁ£ºJava¼¯ºÏ½Ó¿Ú×ÛÊö

·¢²¼Ê±¼ä:2006-07-08 09:16:47À´Ô´:ºìÁª×÷Õß:Çç
¡¡¡¡¼¯ºÏ×ÜÊÇÐèÒªµü´úµÄ£¬ÎÒÃÇȷʵÐèҪ̽²ì¼¯ºÏÖеÄÿһ¸öÔªËØ£¬ËùÒÔ¼¯ºÏ½Ó¿Ú¶¼ÎÞÒ»ÀýÍâµÄ¼Ì³ÐÁËIterable ½Ó¿Ú £¬¶ø¸Ã½Ó¿ÚµÄΨһ·½·¨ £º

Iterator iterator()
·µ»ØÒ»¸öÔÚÒ»×é T ÀàÐ͵ÄÔªËØÉϽøÐеü´úµÄµü´úÆ÷¡£

¡¡¡¡¿ÉÒÔ·µ»ØÒ»¸öËùÓм¯ºÏ½Ó¿Ú¶¼¼Ì³ÐÁ˵ÄIteraor½Ó¿Ú£¬Èç´Ë¼¯ºÏ¿ò¼ÜµÄËùÓÐ×ÓÀ඼¿ÉÒÔµü´úÏÔʾÆäÔªËØ£¡

¡¡¡¡ÀýÈçÏ£º¼¯ºÏµÄ¸ù½Ó¿ÚCollection¼Ì³ÐÁËIterator½Ó¿Ú£º

public interface Collection
extends Iterable
¡¡¡¡Collection ²ã´Î½á¹¹ Öеĸù½Ó¿Ú¡£Collection ±íʾһ×é¶ÔÏó£¬ÕâЩ¶ÔÏóÒ²³ÆΪ collection µÄÔªËØ¡£Ò»Ð© collection ÔÊÐíÓÐÖظ´µÄÔªËØ£¬¶øÁíһЩÔò²»ÔÊÐí¡£Ò»Ð© collection ÊÇÓÐÐòµÄ£¬¶øÁíһЩÔòÊÇÎÞÐòµÄ¡£JDK ²»Ìṩ´Ë½Ó¿ÚµÄÈκÎÖ±½Ó ʵÏÖ£ºËüÌṩ¸ü¾ßÌåµÄ×Ó½Ó¿Ú£¨Èç Set ºÍ List£©ÊµÏÖ¡£´Ë½Ó¿Úͨ³£ÓÃÀ´´«µÝ collection£¬²¢ÔÚÐèÒª×î´óÆÕ±éÐԵĵط½²Ù×÷ÕâЩ collection¡£ ¡¡¡¡°ü (bag) »ò¶à¼¯ºÏ (multiset)£¨¿ÉÄÜ°üº¬Öظ´ÔªËصÄÎÞÐò collection£©Ó¦¸ÃÖ±½ÓʵÏִ˽ӿڡ£

¡¡¡¡ËùÓÐͨÓÃµÄ Collection ʵÏÖÀࣨͨ³£Í¨¹ýËüµÄÒ»¸ö×Ó½Ó¿Ú¼ä½ÓʵÏÖ Collection£©Ó¦¸ÃÌṩÁ½¸ö¡°±ê×¼¡±¹¹Ôì·½·¨£ºÒ»¸öÊÇ void£¨ÎÞ²ÎÊý£©¹¹Ôì·½·¨£¬ÓÃÓÚ´´½¨¿Õ collection£»ÁíÒ»¸öÊÇ´øÓÐ Collection ÀàÐ͵¥²ÎÊýµÄ¹¹Ôì·½·¨£¬ÓÃÓÚ´´½¨Ò»¸ö¾ßÓÐÓëÆä²ÎÊýÏàͬԪËØÐ嵀 collection¡£Êµ¼ÊÉÏ£¬ºóÕßÔÊÐíÓû§¸´ÖÆÈκΠcollection£¬ÒÔÉú³ÉËùÐèʵÏÖÀàÐ͵ÄÒ»¸öµÈЧ collection¡£¾¡¹ÜÎÞ·¨Ç¿ÖÆÖ´ÐдËÔ¼¶¨£¨ÒòΪ½Ó¿Ú²»ÄÜ°üº¬¹¹Ôì·½·¨£©£¬µ«ÊÇ Java ƽ̨¿âÖÐËùÓÐͨÓÃµÄ Collection ʵÏÖ¶¼×ñ´ÓËü¡£

¡¡¡¡´Ë½Ó¿ÚÖаüº¬µÄ¡°ÆÆ»µÐÔ¡±·½·¨£¬ÊÇÖ¸¿ÉÐÞ¸ÄÆäËù²Ù×÷µÄ collection µÄÄÇЩ·½·¨£¬Èç¹û´Ë collection ²»Ö§³Ö¸Ã²Ù×÷£¬ÔòÖ¸¶¨ÕâЩ·½·¨Å׳ö UnsupportedOperationException¡£Èç¹ûÊÇÕâÑù£¬ÄÇôÔÚµ÷ÓöԸà collection ÎÞЧʱ£¬ÕâЩ·½·¨¿ÉÄÜ£¬µ«²¢²»Ò»¶¨Å׳ö UnsupportedOperationException¡£ÀýÈ磬Èç¹ûÒªÌí¼ÓµÄ collection Ϊ¿ÕÇÒ²»¿ÉÐ޸ģ¬Ôò¶Ô¸Ã collection µ÷Óà addAll(Collection) ·½·¨Ê±£¬¿ÉÄܵ«²¢²»Ò»¶¨Å׳öÒì³£¡£

¡¡¡¡Ò»Ð© collection ʵÏÖ¶ÔËüÃÇ¿ÉÄÜ°üº¬µÄÔªËØÓÐËùÏÞÖÆ¡£ÀýÈ磬ijЩʵÏÖ½ûÖ¹ null ÔªËØ£¬¶øijЩʵÏÖÔò¶ÔÔªËصÄÀàÐÍÓÐÏÞÖÆ¡£ÊÔͼÌí¼Ó²»ºÏ¸ñµÄÔªËؽ«Å׳öÒ»¸öδ¾­¼ì²éµÄÒì³££¬Í¨³£ÊÇ NullPointerException »ò ClassCastException¡£ÊÔͼ²éѯÊÇ·ñ´æÔÚ²»ºÏ¸ñµÄÔªËØ¿ÉÄÜÅ׳öÒ»¸öÒì³££¬»òÕßÖ»ÊǼòµ¥µØ·µ»Ø false£»Ä³Ð©ÊµÏÖ½«±íÏÖ³öÇ°Ò»ÖÖÐÐΪ£¬¶øijЩʵÏÖÔò±íÏÖºóÒ»ÖÖ¡£½ÏΪ³£¼ûµÄÊÇ£¬ÊÔͼ¶Ôij¸ö²»ºÏ¸ñµÄÔªËØÖ´ÐвÙ×÷ÇҸòÙ×÷µÄÍê³É²»»áµ¼Ö½«²»ºÏ¸ñµÄÔªËزåÈë collection ÖУ¬½«¿ÉÄÜÅ׳öÒ»¸öÒì³££¬Ò²¿ÉÄܲÙ×÷³É¹¦£¬ÕâÈ¡¾öÓÚʵÏÖ±¾Éí¡£ÕâÑùµÄÒì³£Ôڴ˽ӿڵĹ淶Öбê¼ÇΪ¡°¿ÉÑ¡¡±¡£

¡¡¡¡´Ë½Ó¿ÚÊÇ Java Collections Framework µÄ³ÉÔ±¡£

¡¡¡¡Collections Framework ½Ó¿ÚÖеĺܶ෽·¨ÊǸù¾Ý equals ·½·¨¶¨ÒåµÄ¡£ÀýÈ磬contains(Object o) ·½·¨µÄ¹æ·¶ÉùÃ÷£º¡°µ±ÇÒ½öµ±´Ë collection °üº¬ÖÁÉÙÒ»¸öÂú×ã (o==null ? e==null :o.equals(e)) µÄÔªËØ e ʱ£¬²Å·µ»Ø true¡£¡±²» Ó¦½«´Ë¹æ·¶Àí½âΪËü°µÖ¸µ÷ÓþßÓзǿղÎÊý o µÄ Collection.contains ·½·¨»áµ¼ÖÂΪÈÎÒâµÄ e ÔªËص÷Óà o.equals(e) ·½·¨¡£¿ÉËæÒâ¶Ô¸÷ÖÖʵÏÖÖ´ÐÐÓÅ»¯£¬Ö»Òª±ÜÃâµ÷Óà equals ¼´¿É£¬ÀýÈ磬ͨ¹ýÊ×ÏȱȽÏÁ½¸öÔªËصĹþÏ£Âë¡££¨Object.hashCode() ¹æ·¶±£Ö¤¹þÏ£Âë²»ÏàµÈµÄÁ½¸ö¶ÔÏó²»»áÏàµÈ£©¡£½ÏΪ³£¼ûµÄÊÇ£¬¸÷ÖÖ Collections Framework ½Ó¿ÚµÄʵÏÖ¿ÉËæÒâÀûÓûù´¡ Object ·½·¨µÄÖ¸¶¨ÐÐΪ£¬¶ø²»¹ÜʵÏÖ³ÌÐòÈÏΪËüÊÇ·ñºÏÊÊ¡£

¡¡¡¡¼¯ºÏCollectionµÄ¸¸½Ó¿ÚËù·µ»ØµÄIterator½Ó¿ÚµÄÒâÒ壺

public interface Iterator¡¡¡¡¶Ô¼¯ºÏ½øÐеü´úµÄµü´úÆ÷¡£µü´úÆ÷´úÌæÁË Java Collections Framework ÖÐµÄ Enumeration¡£µü´úÆ÷Óëö¾ÙÓÐÁ½µã²»Í¬£º
1.µü´úÆ÷ÔÊÐíµ÷Ó÷½ÀûÓö¨ÒåÁ¼ºÃµÄÓïÒåÔÚµü´úÆÚ¼ä´Óµü´úÆ÷ËùÖ¸ÏòµÄ¼¯ºÏÒƳýÔªËØ¡£
2.·½·¨Ãû³ÆµÃµ½Á˸Ľø¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ