链表中元素的频率

时间:2014-08-29 11:29:25

标签: java

我有一个类型点的链接列表我想计算特定点的频率
代码

     LinkedList<Point> refernce = new LinkedList<>();
      Point neworigin = new Point();
public void distancecalculator(char [][]problem ,LinkedList<Point> refernce) {

  //Somewhere in my code
         for(int i = 0; i < 4; i++)
         {

              int a = reference.x + x[i];   // x={ 0 , 0 ,1 , -1}

              int b = reference.y + y[i];    // y ={ 1, -1 , 0 ,0}

              neworigin.x = a;
             neworigin.y = b;
            reference.add(neworigin)
        if(Collections.frequency(refernce, neworigin) < 6)
        {
                   //End the that thread
        }
   else
      {
           solver s = new solver(newproblerm , refernce );

                          som =  new Thread(s);
                          som.start();
       }

} }

错误:

  at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-742" java.lang.NullPointerException
    at java.util.LinkedList$ListItr.next(Unknown Source)
        at java.util.Collections.frequency(Unknown Source)

提升它 请帮助我。

1 个答案:

答案 0 :(得分:1)

您正在使用来自多个主题的LinkedListLinkedList的{​​{3}}明确指出(粗体):

  

请注意,此实现未同步。如果多个线程同时访问链表,并且至少有一个线程在结构上修改了列表,则必须在外部进行同步。

你的帖子名称“吓到”我:

  

线程中的异常“Thread- 742 ”java.lang.NullPointerException

看起来你有很多(数百?)的线程。您同时访问LinkedList并且其内部状态被破坏的可能性很高。

然后Collections.frequency()尝试使用其迭代器遍历列表,该迭代器在其实现中达到null值可能是由于LinkedList被破坏。

如果没有正确的同步,请不要使用多个线程中的LinkedList

相关问题