用于读取数据集的代码

时间:2014-02-28 07:04:48

标签: java decision-tree

在这里,我读取数据集并提取数据行(而不是属性)并打印出来。接下来我需要对数据集进行排序。现在这是存储在ArrayList中的。如何对其进行排序?

public static void main(String args[]) throws Exception 

{
    String filen, jsnfl;
    Customiseddata data = new Customiseddata();
    data.setAlgorithm("C4.5");
    data.setUserName("Dahlia");

    System.out.println("Enter the file name");
    sc = new Scanner(System.in);
    filen = sc.nextLine();
    data.setFileName("input_files/" + filen);
    Mainclass main = new Mainclass();
    main.build(data);
}



public void build(Customiseddata data) throws Exception 
{
        int extension;
        String filename;
        filename = data.getFileName();
        extension = filename.lastIndexOf('.');
        String extensionType = filename.substring(extension + 1,
                filename.length());
        if (extensionType.equalsIgnoreCase("csv")) 
        {
            readcsv(filename);
        }

        else if (extensionType.equalsIgnoreCase("arff")) 
        {
            readarff(filename);
        }
}
public void readarff(String filename) throws Exception
{
    @SuppressWarnings("unused")
    int filesize, attributesize, c = 0, i;
    @SuppressWarnings("unused")
    float v = 0;
    String s, line1;
    ArrayList<String> filelines;
    ArrayList<String> attributes;
    Customiseddata data = new Customiseddata();

    Arfffilereader arfffile = new Arfffilereader();
    Extractdata exdata = new Extractdata();
    exdata = arfffile.extractInputArff(filename);

    filelines = exdata.getFileLines();
    attributes = exdata.getAttributes();
    filesize = filelines.size();
    attributesize = attributes.size();
    data.setFilesize(filesize);
    System.out.println("Print the attributes");
    System.out.println("--------------------");
    for (i = 0; i < attributesize; i++) 
            {
        System.out.println(attributes.get(i));
    }
    System.out.println("\t");
    System.out.println("Print the filelines");
    System.out.println("--------------------");
    for (int j = 0; j < filesize; j++) 
            {
        System.out.println(filelines.get(j));
    }
}

但在此之后我需要对数据集进行排序。

1 个答案:

答案 0 :(得分:2)

由于列表的元素是String,而String实现了Comparable,因此对列表进行排序非常简单:

Collections.sort(theList);

但请注意,它会对列表进行排序。如果您不想这样做,请复制一份列表并对该副本进行排序。