Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collections.sort compile error - incompatible types

Tags:

java

I have been developing an implementation of the neighbourhood algorithm in Java for a physics project I am working on. I'm brand new to Java so I apologize for any idiocy that results.

I have been getting the error ''

incompatible types
found   : void
required: java.util.List<VoronoiPoint>

'' on line 22 from the Java compiler in attempting to compile the program shown below. I cannot figure out why the variable ''thelist'' somehow turns into a void when I declared it to be of type List<VoronoiPoint>. If anybody can explain to me what is going on it would be much appreciated!

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);
  }
}

The other classes I used are here: The VoronoiList class:

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;
  }
}

The VoronoiPoint class:

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;
  }
}

And the parameter boundaries 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];
  }
}
like image 487
Shikamablue Avatar asked Oct 22 '11 20:10

Shikamablue


2 Answers

Collections.sort(..) sorts the original list. It doesn't return a new list. (Its return type is void)

like image 169
Bozho Avatar answered Oct 06 '22 20:10

Bozho


Your code is wrong. Collections.sort() is an in-place sort function; it modifies the given list argument and returns nothing (void).

like image 29
ivy Avatar answered Oct 06 '22 21:10

ivy