2012-04-10 8 views
0

このコードを実行すると、81行目でjava.lang.UnsupportedOperationExceptionが発生します。コード全体を投稿することはベットの慣習に反していますが、コード全体を投稿しない限り、私がやっていること。"main"を取得するjava.lang.UnsupportedOperationException

基本的に、Listからすべての要素が削除されたので、List.removeAll(Collection)を実行しています。私はLine 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); 

    } 
} 

出力/の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

を使用して... – Thilo

+0

これはhttp://sscce.org/恩恵を受ける – andersoj

答えて

6
Arrays.asList(tempArr) 

returnsその配列に連動固定サイズリスト。要素を要素から削除することはできません(または要素を追加することはできません)。

Arrays.asListによって返されたリストは、その配列によって保持されているので、リスト内の要素を更新すると配列内の要素も変更されることに注意してください。

あなたが変更可能なコピーが必要な場合は、スタックトレースが最も参考になる

new ArrayList(Arrays.asList(tempArr))