ConcurrentModificationException Error, Please help


  • 0
    W

    I got an ConcurrentModificationException Error when I use iterator in recursive , Any help would be appreciated!
    I posted my error and code here.

    Runtime Error Message:
    Line 22: java.util.ConcurrentModificationException
    Last executed input:
    "hot"
    "dog"
    ["hot","cog","dog","tot","hog","hop","pot","dot"]

        private int mincount = Integer.MAX_VALUE;
        private Set<String> wordList;
    
        public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
            this.wordList = wordList;
            helper(beginWord,endWord,1);
            return mincount;
        }
        public void helper(String beginWord,String endWord,Integer count)
        {
            if(samewordexceptone(beginWord,endWord))
            {
                mincount = Math.min(++count,mincount);
                return;
            }
            Iterator<String> i = wordList.iterator();
    
            while(i.hasNext()&&wordList.size()>0)
            {
                String wordinlist = i.next();
                if(samewordexceptone(beginWord,wordinlist))
                {
                    i.remove();
                    helper(wordinlist,endWord,count+1);
                }
            }
    
        }
        public boolean samewordexceptone(String beginWord,String endWord)
        {
            int length = beginWord.length();
            int samelettercount = 0;
            char[] endWordCharArray = endWord.toCharArray();
    
            for(int i=0;i<beginWord.length();i++)
            {
                for(int j=0;j<endWord.length();j++)
                {
                    if(beginWord.charAt(i)==endWord.charAt(j)) {
                        samelettercount++;
                        if(samelettercount == length-1) return true;
                        continue;
                    }
                }
            }
            return false;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.