2014-02-21 36 views
0

介绍Overiding的compareTo可比排序

我试图做一个自定义排序方法的字符串数组,但我的代码是不能工作的某些原因。我写作的方法将投入这样

{"/", 
"/games/", 
"/homework/", 
"/usr/", 
"/games/snake/", 
"/temp/downloads/", 
"/usr/local/", 
"/usr/local/bin/"} 

我想排序如下:

  1. 通过目录的数量,即“/”将是第一位的,然后“/游戏/”等
  2. 如果它是一个领带,它应根据最后一个目录按字母顺序排序。

我的代码

import java.util.*; 

public class Dirsort { 
    public String[] sort(String[] dirs) { 
     ArrayList<Sort> mySort = new ArrayList<Sort>(); 

     for (String d: dirs){ 
      String l = d.split("/")[-1]; 
      int di = d.length(); 
      mySort.add(new Sort(di,l,d)); 
     } 
     Collections.sort(mySort); 
     String [] ans = new String [mySort.size()]; 
     int count = 0; 
     for (Sort s: mySort){ 
      ans[count] = s.toString(); 
      count++; 
     } 
     return ans; 
    } 
    class Sort implements Comparable<Sort>{ 
     private int d; 
     private String last; 
     private String dir; 

     public Sort(int myD, String myLast, String myDir){ 
      d = myD; 
      last = myLast; 
      dir = myDir; 
     } 

     public String toString(){ 
      return dir; 
     } 

     @Override 
     public int compareTo(Sort arg0) { 
      // TODO Auto-generated method stub 
      if (this.d == arg0.d){ 
       return this.last.compareTo(arg0.last); 
      } 
      return this.d-arg0.d; 
     } 
    } 
} 

的问题

我收到以下错误,当我测试我的代码,我想不通为什么。

运行时异常:java.lang.ArrayIndexOutOfBoundsException: -1java.lang.ArrayIndexOutOfBoundsException:-1在Dirsort.sort(Dirsort.java:6)在测试$ 1.run(Tester.java:48)” /“,” “/ usr /”,“/ usr/local /”,“/ usr/local/bin /”,“/ games /”,“/ games/snake /”, “/ homework//温度/下载/”]

+3

'String l = d.split(“/”)[ - 1];'数组的索引从'0'变为'length-1'。 –

+2

使用'-1'作为索引不会在Java中工作......我敢打赌,你预计它会返回数组的最后一个元素,除非它不是;) – fge

+0

Perl/Python程序员学习Java? –

回答

0

你不能索引在Java -1阵列。在Python中,您可能已经获得了列表的最后一个元素。欢迎使用Java,在需要存储数组之前,可以使用它的长度作为自己的索引。

String[] dSplit = d.split("/"); 
String l = dSplit [dSplit.length-1]; 
0

你的问题的最简单和更具可读性的实现使用的Comparator代替ComparableComparable更适合你自己的班级。

import java.util.*; 

public class Sort { 
    public static void main(String[] args) { 
     String[] testArray = { 
      "/", 
      "/games/", 
      "/homework/", 
      "/usr/", 
      "/games/snake/", 
      "/temp/downloads/", 
      "/usr/local/", 
      "/usr/local/bin/" 
     }; 

     Comparator<String> pathComparator = new PathComparator(); 
     Arrays.sort(testArray, pathComparator); 

     System.out.println(Arrays.toString(testArray)); 
    } 

    static class PathComparator implements Comparator<String> { 

     public int compare(String path1, String path2) { 
      String[] dirs1 = path1.split("/"); 
      String[] dirs2 = path2.split("/"); 

      if (dirs1.length < dirs2.length) { 
       return -1; 
      } 
      else if (dirs1.length > dirs2.length) { 
       return 1; 
      } 
      else { 
       String lastDir1 = dirs1[dirs1.length - 1]; 
       String lastDir2 = dirs2[dirs2.length - 1]; 

       return lastDir1.compareTo(lastDir2); 
      } 
     } 
    } 
} 

另请注意,使用变量的描述性名称使代码更具可读性。当您的代码包含名为d,l,arg0等变量时,它变得不可读。例如,l可以表示lengthlastlist。在2周内你不会记得它的意思。