2012-04-10 3 views
0

Beim Ausführen dieses Codes erhalte ich eine java.lang.UnsupportedOperationException in Zeile 81. Ich weiß, dass das Posten des gesamten Codes gegen Wettpraktiken ist, aber ich dachte, es wäre ziemlich schwierig zu vermitteln was ich mache, wenn ich nicht den gesamten Code poste."main" erhalten java.lang.UnsupportedOperationException

Grundsätzlich wollte ich alle Vorkommen eines Elements aus einer Liste entfernen, also mache ich List.removeAll (Collection). Ich kann nicht verstehen, was ich in Zeile 81 falsch mache. Danke für die Hilfe!

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); 

    } 
} 

Output/Stacktrace:

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

Ein stacktrace wäre sehr hilfreich ... – Thilo

+0

Dies würde profitieren von einem http://sscce.org/ – andersoj

Antwort

6
Arrays.asList(tempArr) 

returns a feste Größe Liste von diesem Array gesichert. Sie können keine Elemente daraus entfernen (oder Elemente hinzufügen).

Beachten Sie, dass die von Arrays.asList zurückgegebene Liste weiterhin von diesem Array unterstützt wird. Wenn Sie Elemente in der Liste aktualisieren, werden sie auch im Array geändert.

Wenn Sie eine modifizierbare Kopie benötigen, verwenden

new ArrayList(Arrays.asList(tempArr)) 
Verwandte Themen