在没有比较器的情况下对java中的路径名数组列表进行排序

时间:2016-03-11 07:41:58

标签: java sorting arraylist

我的用户输入是

  

\家\我\ CS1

     

\ USR \共享

     

\变种\日志

     

\ USR \本地\ jdk1.6.0 \ JRE \ lib中

我需要对这些路径名进行排序,以使输出符合正确的词法顺序。但是,它们首先按长度排序,长度是每个字符串中的斜杠数。路径名存储在字符串的arraylist中。我尝试在不使用集合,比较器或数组的情况下执行此操作。使用ArrayList会有可能吗?

输出应为:

  

\ USR \共享

     

\变种\日志

     

\家\我\ CS1

     

\ USR \本地\ jdk1.6.0 \ JRE \ lib中

到目前为止,这是我的代码:

import java.util.ArrayList;
import java.util.Scanner;
public class FileName 
{
private ArrayList<String> pathNames;

public FileName()
{
    pathNames = new ArrayList<String>();
}

public void printPaths()
{
    for(int i = 0; i < pathNames.size(); i++)
    {
        System.out.println(pathNames.get(i));
    }
}
public int pathLength(String path)
{
    int count = 0;
    String slash = "\\";
    for(int i = 0; i < path.length(); i++)
    {
        if(path.substring(i,i + 1).equals(slash))
        {
            count++;
        }   
    }
    return count;
}

public void sort()
{
    pathNames = mergeSort(pathNames);
}
public ArrayList<String> mergeSort(ArrayList<String> paths)
{
    if(paths.size() == 1)
    {
        return paths;
    }
    else
    {
        ArrayList<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();
        int middle = paths.size() / 2;
        for(int i = 0; i < middle; i++)
        {
            left.add(paths.get(i));
        }

        for(int i = middle; i < paths.size(); i++)
        {
            right.add(paths.get(i));
        }

        right = mergeSort(left);
        left = mergeSort(left);

        merge(left, right, paths);
    }
    return paths;
}

public void merge(ArrayList<String> left, ArrayList<String> right, ArrayList<String> paths)
{
    int leftNum = 0;
    int rightNum = 0;
    int pathsNum = 0;
    while (leftNum < left.size() && rightNum < right.size())
    {
        if ((left.get(leftNum).compareTo(right.get(rightNum)))<0) 
        {
            paths.set(pathsNum, left.get(leftNum));
            leftNum++;
        }
        else
        {
            paths.set(pathsNum, right.get(rightNum));
            rightNum++;
        }
        pathsNum++;
    }

    ArrayList<String>rest;
    int restNum;
    if (leftNum >= left.size()) 
    {
        rest = right;
        restNum = rightNum;
    }
    else 
    {
        rest = left;
        restNum = leftNum;
    }

    for (int i = restNum; i < rest.size(); i++) 
    {
        paths.set(pathsNum, rest.get(i));
        pathsNum++;
    }

}
public void readInput()
{
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a list of path names.(press enter after each path name, and type \"stop\" once you are finished.");
    String termination = "stop";
    String in = input.nextLine();
    boolean reading = true;
    while(reading)
    {
        pathNames.add(in);
        if(in.equals(termination))
        {
            reading = false;
            return;
        }
        in = input.nextLine();
    }
}
}

这是我的主要方法。

public class FileNamePrgm 
{
    public static void main(String [] args)
    {
        FileName paths = new FileName();
        paths.readInput();  
        paths.sort();
    }   
}

1 个答案:

答案 0 :(得分:4)

你有一个错字:

right = mergeSort(left);

应该是

right = mergeSort(right);

此外,您需要在while循环中再次添加in = input.nextLine();。目前,您只从输入中读取一行并反复检查。