gpt4 book ai didi

java - 使用可比排序

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:37 25 4
gpt4 key购买 nike

简介

我使用 Comparable 进行自定义排序的代码无法按照我想要的方式工作。我基本上采用目录数组并对它们进行排序:

  1. 第一个目录数,越少的在先。
  2. 如果按字母顺序是平局。

问题

您的输入示例:

["/", "/usr/", "/usr/local/", "/usr/local/bin/", "/games/", "/games/snake/", "/homework/", "/temp/downloads/" ]

应该返回这个:

["/", "/games/", "/homework/", "/usr/", "/games/snake/", "/temp/downloads/", "/usr/local/", "/usr/local/bin/" ]

但由于某种原因,我的代码返回以下内容:

["/", "/usr/", "/games/", "/homework/", "/usr/local/", "/games/snake/", "/usr/local/bin/", "/temp/downloads/" ]

我的代码 [已编辑评论]

import java.util.*;

public class Dirsort { public String[] sort(String[] dirs) {
//Creates Array list containing Sort object
ArrayList<Sort> mySort = new ArrayList<Sort>();
//Loop that gets the 3 needed values for sorting
for (String d: dirs){
String [] l = d.split("/");//String array for alphabetical comparison
int di = d.length();//Length of array for sorting by number of directories
mySort.add(new Sort(di,l,d));//adds Sort object to arraylist (note d (the entire directory) is needed for the toString)
}
Collections.sort(mySort);//sorts according to compareTo
String [] ans = new String [mySort.size()];//Creates a new string array that will be returned
int count = 0;//to keep track of where we are in the loop for appending
for (Sort s: mySort){
ans[count] = s.toString();
count++;
}
return ans;
}
class Sort implements Comparable<Sort>{
private int d;//number of directories
private String [] arr;//array of strings of names of directories
private String dir;//full directory as string for toString

//Constructor
public Sort(int myD, String [] myArr, String myDir){
d = myD;
arr = myArr;
dir = myDir;
}

//toString
public String toString(){
return dir;
}

@Override
public int compareTo(Sort arg0) {
// TODO Auto-generated method stub
//If they are the same return 0
if (this.equals(arg0)){
return 0;
}

//if the directories are empty
if("/".equals(arg0.dir)){
return 1;
}
if ("/".equals(this.dir)){
return -1;
}

//If they are not the same length the shorter one comes first
if (this.d != arg0.d){
return this.d - arg0.d;
}

//If they are the same length, compare them alphabetically
else{
for (int i = 0; i < arg0.d; i++){
if (!this.arr[i].equals(arg0.arr[i])){
return this.arr[i].compareTo(arg0.arr[i]);
}
}
}
return 0;
}
}
}

最佳答案

错误在这里:

for (String d: dirs){
String [] l = d.split("/");

int di = d.length(); // <- here

mySort.add(new Sort(di,l,d));
}

因为您正在比较整个目录字符串的长度,而不是目录中“文件夹”的数量。这就是为什么 "/usr/" 出现在 "/homework/" 之前,例如,因为:

"/usr/".length() == 5
"/homework/".length() == 10

我相信你想要的是这个,使用分割的长度:

int di = l.length;

那么输出是:

//games//homework//usr//games/snake//temp/downloads//usr/local//usr/local/bin/

There's another small bug though (possibly), which is that calling split on a String that starts with the delimiter will result in an empty String at the beginning.

IE:

"/usr/".split("/") == { "", "usr" }

所以你可能想对此做点什么。虽然在这里这意味着它们都以空字符串开头,所以它最终不会影响您进行比较的方式。

顺便说一句,@JBNizet 的建议也是正确的,为变量提供更有意义的名称在这里会有很大帮助。 fullDir.length()splitDir.length 会让这种情况更容易被发现(而且它可能从一开始就从未发生过)。

关于java - 使用可比排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com