在执行此代码时,我在第81行收到了java.lang.UnsupportedOperationException。我知道发布整个代码是违反赌注的做法,但我认为除非我发布整个代码,否则很难传达我正在做的事情代码。
基本上我想从List中删除所有出现的元素,所以我正在做List.removeAll(Collection)。我无法理解我在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
答案 0 :(得分:6)
Arrays.asList(tempArr)
returns由该数组支持的固定大小列表。您无法从中删除元素(或添加元素)。
请注意,Arrays.asList返回的列表仍然由该数组支持,因此当您更新列表中的元素时,它也会在数组中更改它们。
如果您需要可修改的副本,请使用
new ArrayList(Arrays.asList(tempArr))