2012-04-10 54 views
0

在执行此代码,我正在上线81我知道张贴整个代码java.lang.UnsupportedOperationException是对赌的做法,但我认为这将是相当困难的传达我在做什么,除非我发布整个代码。获得“主” java.lang.UnsupportedOperationException

基本上,我想从列表中删除某个元素的所有事件,所以我做List.removeAll(集合)。我无法理解我在81号线上做了什么错误。感谢您的帮助!

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.util.*; 
import java.util.Map.Entry;; 


public class MinCutClass { 

    /** 
    * @param args 
    */ 
    private HashMap verticeMap ; 
    private List edgeList ; 


    public MinCutClass() 
    { 
      verticeMap = new HashMap(); 
      edgeList = new ArrayList(); 
    } 

    public static void main(String[] args) 

    { 
     // TODO Auto-generated method stub 
     MinCutClass minCutObj = new MinCutClass(); 

     minCutObj.populateVertices(); 
     minCutObj.printVerticeMap(); 
     minCutObj.printVerticeMap1(); 

     minCutObj.populateEdges(); 

     //minCutObj.printEdgeList(); 

     minCutObj.findMinCut(); 

//  minCutObj.printEdgeList(); 
    // minCutObj.printVerticeMap(); 


    } 

    private void printEdgeList() 
    { 
     Iterator i = edgeList.iterator(); 

     while(i.hasNext()) 
     { 

      System.out.println(i.next()); 
     } 

    } 

    private void printVerticeMap() 
    { 

     Set s = verticeMap.entrySet(); 
     Iterator i = s.iterator(); 


     while(i.hasNext()) 
     { 
      Entry e = (Entry)i.next(); 

      System.out.println("Key :" + e.getKey() + " Value :" + e.getValue()); 

     } 

    } 

    private void printVerticeMap1() 
    { 
     Collection c = new TreeSet(); 
     c.add("2"); 
     List temp = (List)verticeMap.get("1"); 
     System.out.println(temp.getClass().getName()); 
     temp.removeAll(c); 

    } 

    private void findMinCut() 
    { 


     while (verticeMap.keySet().size() > 2) // as long as there are more than two vertices 
     { 

      int randomEdgeIndex = chooseRandomEdgeIndex(); //choose a random edge basically any random index in edgeList 
      String randomEdgeChosen = (String)edgeList.get(randomEdgeIndex); 

      //Edge contraction 

      //1. remove the edges from edgeList. We want to avoid self loops. There may exist many edges of this type. 
      Collection c = new TreeSet(); 
      c.add(edgeList.get(randomEdgeIndex)); 
      edgeList.removeAll(c); //removeAll , all edges are removed 
      c.clear(); 

      //get edge vertices 
      String [] tempArr = randomEdgeChosen.split("_"); 
      String v1 = tempArr[0]; 
      String v2 = tempArr[1]; 


      //2.a Delete v2 from v1 vertices list, the contracting edge vanishes. Please note, all parallel edges are also being removed as they create self loops. 
      List tempListV1 = (List)verticeMap.get(v1); 
      c.add(v2); 
      tempListV1.removeAll(c); 
      c.clear(); 


      //2.b Now delete v1 from v2 vertices list, the contracting edge and all parallel edges are removed as they create self loops. 
      List tempListV2 = (List)verticeMap.get(v2); 
      c.add(v1); 
      tempListV2.removeAll(c); 
      c.clear(); 

      //3. Now add all vertices v2 is connected with in v1 list because the resultant merged node is v1 
      Iterator i = tempListV2.iterator(); 

      List tempListEle; 

      while (i.hasNext()) 
      { 
       String ele = (String)i.next(); 
       tempListEle = (List)verticeMap.get(ele); //get the vertice list for the current element (from v2 list) being considered as v1 has to be added to that list and v2 removed. 


       tempListV1.add(ele); 
       //tempListV2.remove(ele); //this is not needed , as entry for v2 in verticeMap will be deleted 

       tempListEle.add(v1); 
       tempListEle.remove(v2); 

      } 


      verticeMap.remove(v2); //once all v2 elements are added to entries for all v2 elements are also updated remove entry for v2 in verticeMap 

     } 


    } 

    private int chooseRandomEdgeIndex() 
    { 
     return new Random().nextInt(edgeList.size()); 
    } 

    private void populateVertices() 
    { 
     List list = readFile(); // get a list of String arrays 
     Iterator i = list.iterator(); 
     while(i.hasNext()) 
     { 
      String[] tempArr = (String[])i.next(); // get current String array 
      String node = tempArr[0]; //get the node number 

      tempArr = Arrays.copyOfRange(tempArr, 1, tempArr.length); //create a String array with 0th element removed 


      //System.out.println(node + "" + Arrays.asList(tempArr) + this.verticeMap.getClass().getName()); 
      //System.out.println(node + "" + Arrays.asList(tempArr)); 

      this.verticeMap.put(node, Arrays.asList(tempArr)); // put the node and the nodes it has edges with in a HashMap 

      //System.out.println(node + "" + verticeMap.get(node).toString()); 

     } 


     //System.out.println("1" + "" + verticeMap.get("1").toString()); 

    } 

    private void populateEdges() 
    { 
     List list = readFile(); // get a list of String arrays 
     Iterator i = list.iterator(); 
     while(i.hasNext()) 
     { 
      String[] tempArr = (String[])i.next(); // get current String array 
      String node = tempArr[0]; //get the node number 

      for(int count = 1 ; count <= tempArr.length - 1; count++) 
      { 
       if(getInt(node) < getInt(tempArr[count])) //add the edge to the edgeList only if the node is smaller than other node being considered. This way you only add each edge only once for each pair of vertices. 
       { 

        //System.out.println(node + tempArr[count]); 
        edgeList.add(""+node+"_"+tempArr[count]); 

       } 
      } 
     } 

//  i = edgeList.iterator(); 
//  while (i.hasNext()) 
//  { 
//   System.out.println(i.next().toString()); 
//   
//  } 

    } 

    private List readFile() 
    { 
     List list = new ArrayList(); // list of String arrays 
      try 
      { 
       FileInputStream fstream = new FileInputStream("C:/Users/ankura/Desktop/KargerAdj.txt"); 

        DataInputStream in = new DataInputStream(fstream); 
        BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
        String strLine; 
        String[] strArr; 
        while ((strLine = br.readLine()) != null) 
        { 

         strLine = strLine.trim(); 
         strArr = strLine.split("\\W+"); 

         list.add(strArr); 

        } 

        in.close(); 

      } 

      catch (Exception e) 
      { 
       //Catch exception if any 
       System.err.println("Error: " + e.getMessage()); 
      } 

      return list; 

    } 


    public int getInt(Object o) 
    { 

     return Integer.parseInt((String)o); 

    } 
} 

输出/堆栈跟踪:

Exception in thread "main" java.lang.UnsupportedOperationException 
    at java.util.AbstractList.remove(Unknown Source) 
    at java.util.AbstractList$Itr.remove(Unknown Source) 
    at java.util.AbstractCollection.removeAll(Unknown Source) 
    at MinCutClass.printVerticeMap1(MinCutClass.java:81) 
    at MinCutClass.main(MinCutClass.java:32) 
Key :3 Value :[2, 4] 
Key :2 Value :[1, 3, 4] 
Key :1 Value :[2, 4] 
Key :4 Value :[1, 2, 3] 
java.util.Arrays$ArrayList 
+1

堆栈跟踪将是最有帮助的... – Thilo 2012-04-10 02:28:09

+0

这从http://sscce.org/受益 – andersoj 2012-04-10 02:33:27

回答

6
Arrays.asList(tempArr) 

returns一个固定大小列表由该阵列的支持。您不能从中删除元素(或添加元素)。

注意,由Arrays.asList返回列表仍然由阵列的支持,所以,当你更新列表中的元素,它会在数组中改变他们。

如果需要修改副本,使用

new ArrayList(Arrays.asList(tempArr)) 
相关问题