2011-10-22 5 views
3

저는 작업중인 물리 프로젝트에서 Java로 이웃 알고리즘의 구현을 개발했습니다. 나는 새로운 자바에 익숙해 져서 그 결과에 대해 사과합니다.Collections.sort 컴파일 오류 - 호환되지 않는 유형

나는 아래의 프로그램을 컴파일 시도에서 자바 컴파일러에서 라인 (22)에 오류 ''

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

''를 받고있다. 변수 'thelist'가 왜 List<VoronoiPoint> 유형으로 선언 될 때 어떻게 든 void로 변하는 이유를 알 수 없습니다. 아무도 나 한테 무슨 일이 일어 났는지 설명 할 수 없다면 크게 감사하겠습니다. 내가 사용

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).

관련 문제