【发布时间】:2019-10-13 11:47:01
【问题描述】:
我正在尝试编写一个程序来分析和解释 GIS 数据。我已经将程序完全编码,但是当我尝试使用 compareTo 根据不同的指标(海拔、纬度等)对程序进行排序时遇到了问题。我不断收到错误消息“集合类型中的方法排序(列表)不适用于参数(GISDataStructure”。
我关注的教程和指南都有主类将数据添加到 main 中的数组列表,而我正在调用不同的类,并使用 lineData 从 .txt 文件中收集信息,并查看它,实例化每个排。然后我从不同的类调用一个方法并插入数据。我目前在我的数据类中编写了一个基本的 compareTo,我在其中获取和设置所有参数,但是在尝试使用 compareTo 的不同方法时不断出错,甚至在不同的类中编写基本排序方法。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class GISRunner {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException {
File file = new File("GIS.txt");
Scanner input = new Scanner(file);
String[] lineData = null;
String lineRaw = null;
String featureName, featureClass, stateCode, countyName, primaryLatitudeDMS, primaryLongitudeDMS,
sourceLatitudeDMS, sourceLongitudeDMS, mapName;
Integer featureId, stateId, countyId, elevationMeters, elevationFeet;
Double primaryLatitudeDecimal, primaryLongitudeDecimal, sourceLatitudeDecimal, sourceLongitudeDecimal;
GISDataStructure datas = new GISDataStructure();
while (input.hasNext()) {
lineRaw = input.nextLine();
lineData = lineRaw.split("\\|");
featureId = 0;
try {
featureId = Integer.valueOf(lineData[0]);
} catch (NumberFormatException e) {
featureId = Integer.valueOf(lineData[0].substring(3));
}
featureName = lineData[1];
featureClass = lineData[2];
stateCode = lineData[3];
stateId = Integer.valueOf(lineData[4]);
countyName = lineData[5];
try {
countyId = Integer.valueOf(lineData[6]);
} catch (NumberFormatException e2) {
countyId = null;
}
primaryLatitudeDMS = lineData[7];
primaryLongitudeDMS = lineData[8];
primaryLatitudeDecimal = Double.valueOf(lineData[9]);
primaryLongitudeDecimal = Double.valueOf(lineData[10]);
sourceLatitudeDMS = lineData[11];
sourceLongitudeDMS = lineData[12];
try {
sourceLatitudeDecimal = Double.valueOf(lineData[13]);
} catch (NumberFormatException e1) {
sourceLatitudeDecimal = null;
}
try {
sourceLongitudeDecimal = Double.valueOf(lineData[14]);
} catch (NumberFormatException e1) {
sourceLongitudeDecimal = null;
}
try {
elevationMeters = Integer.valueOf(lineData[15]);
} catch (NumberFormatException e) {
elevationMeters = null;
}
try {
elevationFeet = Integer.valueOf(lineData[16]);
} catch (NumberFormatException e) {
elevationFeet = null;
}
mapName = lineData[17];
GISData newGISData = new GISData(featureId, featureName, featureClass, stateCode, stateId, countyName,
countyId, primaryLatitudeDMS, primaryLongitudeDMS, primaryLatitudeDecimal, primaryLongitudeDecimal,
sourceLatitudeDMS, sourceLongitudeDMS, sourceLatitudeDecimal, sourceLongitudeDecimal,
elevationMeters, elevationFeet, mapName);
datas.insert(newGISData);
}
System.out.println(datas.toString());
input.close();
Collections.sort(datas);
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import edu.matc.itdev154.finalexam.GISData.SortByType;
public class GISDataStructure {
private static Scanner sc = new Scanner(System.in);
public ArrayList<GISData> data;
public GISDataStructure() {
data = new ArrayList<GISData>();
}
public void insert(GISData element) {
data.add(element);
}
public static void searchData(ArrayList<GISData> data) {
String state;
GISData foundData = null;
do {
System.out.print("Enter state code: ");
state = sc.next();
} while (!state.matches("^[a-zA-Z\\s]+"));
for (GISData datas : data) {
if(datas.getStateCode().equals(state)) {
foundData = datas;
}
}
if(foundData != null)
foundData.toString();
else
System.out.println("No data found.");
}
public static void sort(ArrayList<GISData> data) {
Collections.sort(
data, (obj1, obj2) -> obj1.getElevationFeet() - obj2.getElevationFeet()
);
data.forEach(System.out::println);
}
public static void edu(ArrayList<GISData> data) {
GISData min1 = data.stream()
.min(Comparator.comparingInt(GISData::getElevationFeet))
.get();
System.out.println("Area min elevation feet: " + min1);
GISData max1 = data.stream()
.max(Comparator.comparingInt(GISData::getElevationFeet))
.get();
System.out.println("Area max elevation feet: " + max1);
}
@Override
public String toString() {
StringBuilder retString = new StringBuilder();
for (GISData gis : data) {
retString.append(gis.toString());
}
return retString.toString();
}
}
/**
*
*/
package edu.matc.itdev154.finalexam;
import java.util.Date;
public class GISData implements Comparable<GISData> {
public static enum SortByType {CountyName,
FeatureName,
PrimaryLatitude,
PrimaryLongitude,
SourceLatitude,
SourceLongitude,
ElevationFeet};
private int featureId;
private String featureName;
private String featureClass;
private String stateCode;
private int stateId;
private String countyName;
private Integer countyId;
private String primaryLatitudeDMS;
private String primaryLongitudeDMS;
private double primaryLatitudeDecimal;
private double primaryLongitudeDecimal;
private String sourceLatitudeDMS;
private String sourceLongitudeDMS;
private Double sourceLatitudeDecimal;
private Double sourceLongitudeDecimal;
private Integer elevationMeters;
private Integer elevationFeet;
private String mapName;
private Date createdDate;
private Date modifiedDate;
private SortByType[] sortBy;
public GISData() {
}
public GISData(int featureId, String featureName, String featureClass, String stateCode, int stateId,
String countyName, Integer countyId, String primaryLatitudeDMS, String primaryLongitudeDMS,
double primaryLatitudeDecimal, double primaryLongitudeDecimal, String sourceLatitudeDMS, String sourceLongitudeDMS, Double sourceLatitudeDecimal,
Double sourceLongitudeDecimal, Integer elevationMeters, Integer elevationFeet, String mapName) {
this.featureId = featureId;
this.featureName = featureName;
this.featureClass = featureClass;
this.stateCode = stateCode;
this.stateId = stateId;
this.countyName = countyName;
this.countyId = countyId;
this.primaryLatitudeDMS = primaryLatitudeDMS;
this.primaryLongitudeDMS = primaryLongitudeDMS;
this.primaryLatitudeDecimal = primaryLatitudeDecimal;
this.primaryLongitudeDecimal = primaryLongitudeDecimal;
this.sourceLatitudeDMS = sourceLatitudeDMS;
this.sourceLongitudeDMS = sourceLongitudeDMS;
this.sourceLatitudeDecimal = sourceLatitudeDecimal;
this.sourceLongitudeDecimal = sourceLongitudeDecimal;
this.elevationMeters = elevationMeters;
this.elevationFeet = elevationFeet;
this.mapName = mapName;
}
// Getters and Setters
@Override
public int compareTo(GISData o) {
return this.elevationFeet - o.elevationFeet;
}
我希望程序循环显示每行数据的实例化,然后调用我的 Collections.sort 之后的另一个循环,我会在其中为每个语句编写一个。
【问题讨论】:
-
错误很明显:您尝试调用
Collections.sort(datas);但datas不是List。 -
在
GISDataStructure中你已经有一个sort方法——你为什么不调用那个方法? -
您只能在集合上调用
Collections.sort()。GISDataStructure对象不是集合。 -
@Scary Wombat 我的 GISDataStructure 排序方法将无法在 main 中正确执行。当不传递任何参数时,我得到一个“GISDataStructure 类型中的方法 sort(ArrayList
) 不适用于参数 ()”。 -
GISDataStructure.sort(datas.data);
标签: java sorting object arraylist compareto