当前位置: 首页 > 面试题库 >

获取“主要” java.lang.UnsupportedOperationException

弘浩瀚
2023-03-14
问题内容

在执行此代码时,我在第81行上收到了java.lang.UnsupportedOperationException。我知道发布整个代码违反了赌注的做法,但是我认为除非发布整个代码,否则传达我正在做的事情将非常困难。

基本上,我想从列表中删除所有出现的元素,所以我正在做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

问题答案:
  Arrays.asList(tempArr)

返回由该数组支持的
固定大小的 列表。您不能从中删除元素(或向其中添加元素)。

请注意,由Arrays.asList返回的列表仍受该数组支持,因此当您更新列表中的元素时,它也会在数组中对其进行更改。

如果您需要可修改的副本,请使用

new ArrayList(Arrays.asList(tempArr))


 类似资料:
  • 我有一个使用redux的应用程序 我也曾在本地获取数据。 在我的反应组件中,我使用来获取数据。但我什么也没得到。 如何在我的组件中获取数据? 演示:https://codesandbox.io/s/nervous-rosalind-lp16j?file=/src/App.js:242-328

  • 我有xml,我需要解救: 我可以很容易地整理它,但是当我在另一边尝试时,我得到null Item。我的主要类别是: 然后 ...等等 我使用Java12,我的pom依赖项中有jaxb-api和glassfish.jaxb。我试图将@XMLElement注释放在getter和setter上。我试图将XMLRootElement注释放在每个嵌套类上,但没有运气。我还尝试不使用@Builder,也不初始

  • 问题内容: 我试图连接到在WebSphere上运行的Java应用程序内的Oracle数据库。我需要能够创建一个数组描述符以在对过程的调用中使用。 代码如下: 调用的行引发类强制转换异常 通过调试器进行遍历,可以肯定是一个。问题是我实际上不能在我的代码中引用该类,因为该类不是公共的,所以我不能只做这样的事情: 。 还有这个: 还返回类强制转换异常: 我需要有一个对象,但似乎无法从返回给我的对象中得到

  • 问题内容: 我有一组以矩阵形式表示的2D向量。 我希望得到第一个主成分,即指示方差最大的方向的向量。 我从莱斯大学那里找到了一个相当详细的文档。 基于此,我已导入数据并执行以下操作: 然后,如何获得作为第一主要成分的3D矢量? 问题答案: PCA仅从2d数据提供2d vecs。 看一下Wikipedia PCA中的图片: 从这样的点云(dataMatrix)开始,然后使用, 是第一台PC,即图中的

  • 我正在制作自己的Start/BookmarkPage作为一个业余项目。 我想以一种干净的方式组织我的书签。我喜欢苹果通过请求meta应用程序图标来完成这一工作的方式,所以我制作了一个JavaScript/Ajax/PHP函数来完成这项工作。 然而,当一个网站的头部没有应用程序图标时,我想知道该网站使用的主颜色,就像你在这里看到的那样 safari上的苹果书签(背景有网站的主色) chrome书签(

  • X2.2.0新增 sp_get_current_theme($default_theme='') 功能: 获取当前主题名 参数: $default_theme: 指定的默认主题 返回: 类型string,主题名 使用: $theme = sp_get_current_theme();