Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用比较器而不添加类_Java_String_Sorting_Comparator - Fatal编程技术网

Java 使用比较器而不添加类

Java 使用比较器而不添加类,java,string,sorting,comparator,Java,String,Sorting,Comparator,我正在尝试按字符串长度对arraylist排序,我知道实现了Comparator,但我想知道这是否可以在我的函数中完成,而不添加任何额外的类或方法?理想情况下,我想将它们从最短输出到最长,但我可以做到 下面是我想用以实现比较器的方法的一个片段 public static void sCompare(BufferedReader r, PrintWriter w) throws IOException { ArrayList<String> s= new ArrayList&

我正在尝试按字符串长度对arraylist排序,我知道实现了Comparator,但我想知道这是否可以在我的函数中完成,而不添加任何额外的类或方法?理想情况下,我想将它们从最短输出到最长,但我可以做到

下面是我想用以实现比较器的方法的一个片段

public static void sCompare(BufferedReader r, PrintWriter w) throws IOException {

    ArrayList<String> s= new ArrayList<String>();

    String line;
    int n = 0;
    while ((line = r.readLine()) != null) {
        s.add(line);
        n++;
    }
    //Collections.sort(s);  

    Iterator<String> i = s.iterator();
    while (i.hasNext()) {
        w.println(i.next());
    }
  }
publicstaticvoidscompare(BufferedReader r,PrintWriter w)抛出IOException{
ArrayList s=新的ArrayList();
弦线;
int n=0;
而((line=r.readLine())!=null){
s、 添加(行);
n++;
}
//收集、分类;
迭代器i=s.Iterator();
while(i.hasNext()){
w、 println(i.next());
}
}

提前感谢您的任何意见

我认为实现
比较器
接口没有任何错误。 如果您只关心函数中的所有操作,那么可以使用匿名实现。大致如下:

    Collections.sort(s, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.length() - o2.length();
        }
    });  
Collections.sort(s,新的Comparator(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回o1.length()-o2.length();
}
});  
(这将替换当前行
//Collections.sort;

PS:您从不使用
n
的值

PPS:根据您在
return
语句中想要的顺序,您可能必须反转o1和o2


另一个例子是

我认为实现
比较器
接口没有任何错误。 如果您只关心函数中的所有操作,那么可以使用匿名实现。大致如下:

    Collections.sort(s, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.length() - o2.length();
        }
    });  
Collections.sort(s,新的Comparator(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回o1.length()-o2.length();
}
});  
(这将替换当前行
//Collections.sort;

PS:您从不使用
n
的值

PPS:根据您在
return
语句中想要的顺序,您可能必须反转o1和o2

的另一个例子,我假设“类”是指“顶级类”,因此允许使用:

Collections.sort(s,新的Comparator(){
公共整数比较(字符串a、字符串b){
//java 1.7:
返回整数。比较(a.length(),b.length());
//java 1.6
返回a.length()-b.length();
}
});
我假设“类”是指“顶级类”,因此允许使用:

Collections.sort(s,新的Comparator(){
公共整数比较(字符串a、字符串b){
//java 1.7:
返回整数。比较(a.length(),b.length());
//java 1.6
返回a.length()-b.length();
}
});


您可以使用循环实现排序逻辑,但为什么不使用comparator?如果需要,您可以使用匿名类。@JigarJoshi我正在寻找最有效的方法。只需要使用一个类。有没有一个有效的解决方案?添加另一个类不会影响效率,
Collections.sort()
nlog(n)
第一部分获取文件,并为每一行生成其长度,后跟一个空格,后跟行。第二部分对第1列(长度)进行数字排序。最后一部分通过将数字序列和空格替换为空来消除长度列。这些都是非常基本的外壳材料;如果你不熟悉,那没关系。但是它非常值得学习IMHO。你可以用循环实现排序逻辑,但是为什么不使用comparator呢?如果你愿意,你可以使用匿名类。@JigarJoshi我正在寻找最有效的方法。只需要使用一个类。有没有一个有效的解决方案?添加另一个类不会影响效率,
Collections.sort()
nlog(n)
第一部分获取文件,并为每一行生成其长度,后跟一个空格,后跟行。第二部分对第1列(长度)进行数字排序。最后一部分通过将数字序列和空格替换为空来消除长度列。这些都是非常基本的外壳材料;如果你不熟悉,那没关系。但是它是如此值得学习。谢谢你的帮助@Bohemian。你知道这个错误是什么意思吗?它来自Java1.7中添加的codeInteger.compare最后一行的“.compare”。你在用1.6吗?谢谢你的帮助@Bohemian。你知道这个错误是什么意思吗?它来自Java1.7中添加的codeInteger.compare最后一行的“.compare”。您使用的是1.6吗?是否可以对其进行编辑以处理相同长度的字符串按自然顺序进行分拣的情况?谢谢您接受我的回答。你说的“自然秩序”是什么意思?你想要哪个顺序?假设我有{hello,heellooo,apple,appppplleee}输出是apple hello heellooo appppplleee。。。所以基本上,长度相同的情况下,我看到的是字母顺序。你想要什么?长度相同的单词是否按反字母顺序排列?或者你指的是某种类型的?我想,对于相同长度的单词,可以对其进行编辑以处理相同长度的字符串按自然顺序排序的情况吗?谢谢你接受我的回答。你说的“自然秩序”是什么意思?你想要哪个顺序?假设我有{hello,heellooo,apple,appppplleee}输出是apple hello heellooo appppplleee。。。所以基本上,长度相同的情况下,我看到的是字母顺序。你想要什么?长度相同的单词是否按反字母顺序排列?或者你指的是某种类型的?我想,对于相同长度的单词,使用普通的语法