当前位置: 代码迷 >> J2SE >> hash地图的有关问题
  详细解决方案

hash地图的有关问题

热度:170   发布时间:2016-04-23 20:14:17.0
hashmap的问题
    private abstract class HashIterator<E> implements Iterator<E> {  
        Entry<K,V> next;  
        int expectedModCount;   
        int index;      
        Entry<K,V> current;   
  
        // 构造方法  
        HashIterator() {  
            expectedModCount = modCount;  
            if (size > 0) {  
                Entry[] t = table;  
                while (index < t.length && (next = t[index++]) == null)  
                    ;  
            }  
        }  
...}
上面是hashmap的迭代器代码,index不需要初始化就在构造函数里用了是什么情况?
------解决思路----------------------
int 是基础类型,初始化为0
------解决思路----------------------
数据类型,如果直接写在类中作为属性被定义不需要手动初始化,系统会自动初始化一个值,比如int,就为0,String就为null,boolean就为false.但是如果定义在方法里就必须手动初始化才能被使用。