Collections.sort编译错误 - 不兼容的类型

时间:2011-10-22 20:42:53

标签: java

我一直在为我正在研究的物理项目开发Java中的邻域算法的实现。我是Java的新手,所以我为任何愚蠢的结果道歉。

我一直在收到错误 ''

incompatible types
found   : void
required: java.util.List<VoronoiPoint>
尝试编译下面显示的程序时,来自Java编译器的第22行的

''。当我声明变量List<VoronoiPoint>时,我无法弄清楚为什么变量''thelist''会以某种方式变成空白。如果有人能向我解释发生了什么,我将不胜感激!

import java.lang.Double;
import java.util.*;


public class VoronoiTiling 
{


  public static void main(String args[]) 
  {
    Integer n = 10; //Number of dimensions of model parameter space
    Integer ns = 20; //Number of points per iteration
    Integer nr = 4; //Number of cells to populate
    Integer iterations = 5; //Number of iterations
    List<VoronoiPoint> thelist = VoronoiList.startlist(ns,n);
    //System.out.println(thelist);
    //System.out.println(thelist.get(1).misfit);
    for (Integer i=0 ; i<thelist.size() ; i++)
    {
      thelist.get(i).setmisfit();
    }
    List<VoronoiPoint> orderedlist = Collections.sort(thelist);
    Double distance = EuclidianDistance((thelist.get(1)).location,(thelist.get(2)).location);
    System.out.println(distance);
  }

  public static Double EuclidianDistance(Double[] point1, Double[] point2)
  {
    Double distance=0.0;
    for (int i = 0; i < point1.length; i++)
    {
      distance = distance + Math.pow((point1[i]-point2[i]),2);
    }
  return Math.sqrt(distance);
  }
}

我使用的其他课程在这里: VoronoiList类:

import java.util.*;

public class VoronoiList
{
  public static List<VoronoiPoint> startlist(Integer ns, Integer n)
  {
    List<VoronoiPoint> thestartlist = new ArrayList<VoronoiPoint>();
    for (int i = 0; i < ns; i++)
    {
      thestartlist.add(new VoronoiPoint(0.,n));
    }
    return thestartlist;
  }
}

VoronoiPoint类:

import java.util.Random;

public class VoronoiPoint implements Comparable<VoronoiPoint>
{
  Double[] location;
  private Random generator = new Random();
  Double misfit = -1.;

  //***************************************************************
  public VoronoiPoint(Double misfit, Integer n)
  {
    location = new Double[n];
    ParameterBoundaries boundaries = new ParameterBoundaries(n);
    for(int i = 0; i < n; i++)
    {
      location[i] = boundaries.getboundaries(2*i)+2*generator.nextDouble();
    }
  }
  //***************************************************************
  //public Double[] getlocation()
  //{
    //return location;
  //}
  public void setlocationi(Integer i, Double j)
  {
    location[i] = j;
  }

  //***************************************************************

  public void setmisfit()
  {
    Integer n = location.length;
    Double tempmisfit = 0.0;
    for(Integer i = 0; i < n; i++)
    {
      tempmisfit = tempmisfit + Math.pow((location[i]),2);
    }
    misfit = Math.sqrt(tempmisfit); // Temporarily just distance to centre
  }

  //public Double getmisfit()
  //{
    //return misfit;
  //}
  public int compareTo(VoronoiPoint b)
  {
    if (this.misfit<b.misfit) return -1;
    else if (this.misfit==b.misfit) return 0;
    return 1;
  }
}

参数boundary class:

public class ParameterBoundaries
{
  private Double[] boundaries; /*Set to 2n where n is dimensions of parameter space, 
   * it just makes it easier*/
  public ParameterBoundaries(Integer n)
  {
    boundaries = new Double[2*n];
    for(Integer i = 0; i<n; i++)
    {
      boundaries[2*i] = -1.0;
      boundaries[2*i+1] = 1.0;
    }
  }
  public Double getboundaries(Integer i)
  {
    return boundaries[i];
  }
}

2 个答案:

答案 0 :(得分:15)

Collections.sort(..)对原始列表进行排序。它不会返回新列表。 (其返回类型为void

答案 1 :(得分:3)

你的代码错了。 Collections.sort()是一个就地排序函数;它修改给定的列表参数并且不返回任何内容(void)。