2011-10-22 40 views
3

我一直在爲我正在開發的物理項目開發Java的鄰域算法的實現。我是Java的新手,所以我對任何結果的白癡道歉。Collections.sort編譯錯誤 - 不兼容的類型

我已經越來越第22行錯誤 '

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

'從Java編譯器試圖編譯如下所示的程序。我無法弄清楚爲什麼當我聲明它是List<VoronoiPoint>類型時,變量「'列表'變成無效的原因。如果有人能向我解釋發生了什麼,我將非常感謝!

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

和參數邊界類:

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]; 
    } 
} 
+0

對於一個制定良好,可回答的問題+1。我希望每個人都會這樣問,提供錯誤信息,相關代碼等。 – OscarRyz

回答

14

Collections.sort(..)對原始列表進行排序。它不會返回一個新的列表。 (其退貨類型爲void

+0

啊謝謝我一定誤會了文檔 – Shikamablue

3

您的代碼有誤。 Collections.sort()是一個就地排序功能;它修改給定的列表參數並返回任何內容(void)。