将compareTo与arrayList一起使用的问题

时间:2019-05-28 01:01:37

标签: java sorting object arraylist compareto

我正在尝试编写一个程序来分析和解释GIS数据。我已将程序完全编码出来,但是当我尝试使用compareTo在不同的指标(海拔,纬度等)上对程序进行排序时遇到了问题。我不断收到错误消息“ Collections类型中的sort(List)方法不适用于自变量(GISDataStructure)。

我所遵循的所有教程和指南都具有主类,该类将数据添加到main中的arraylist中,而我正在调用不同的类,并使用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之后进行另一个循环,在该循环中我为每个语句编写一个。

1 个答案:

答案 0 :(得分:0)

问题在于GISDataStructure不是列表,而是包含列表。由于GISDataStructure不是列表,因此无法调用Collections.sort()。如果要对包含的列表进行排序,则需要对其进行引用。换句话说:

Collections.sort(datas.data);