Java 降序数组列表

Java 降序数组列表,java,arraylist,collections,Java,Arraylist,Collections,在这个程序中,我需要按降序显示外部文件中的数组列表。尽管如此,每当我运行程序时,我仍然得到与原始列表相同的顺序。我只是需要降序部分的帮助。我使用了Collections方法来完成这项任务,但我不认为我完全理解每当我调用Collections时发生的事情,它只是按照第一次数组调用所带来的方式吐出数组列表。任何帮助都将不胜感激。我在问题代码的末尾和开头放了4颗星 import java.io.*; import java.util.*; public class ioStafford {

在这个程序中,我需要按降序显示外部文件中的数组列表。尽管如此,每当我运行程序时,我仍然得到与原始列表相同的顺序。我只是需要降序部分的帮助。我使用了Collections方法来完成这项任务,但我不认为我完全理解每当我调用Collections时发生的事情,它只是按照第一次数组调用所带来的方式吐出数组列表。任何帮助都将不胜感激。我在问题代码的末尾和开头放了4颗星

import java.io.*;
import java.util.*;

public class ioStafford {



    public static void revOrder(String x[])
    {

    }

    public static void main(String args[])
    {
        try(BufferedReader fr1 = new BufferedReader(new FileReader("myImport.txt")))
        {

            //Create a new file for the reverse output if it doesn't exist
            File f1 = new File("myOutput.txt");

            //Code for new file creation
            if(!f1.exists())
            {
                f1.createNewFile();
            }

            //Initialize string variables
            String type;

            //Array List to hold text file
            ArrayList<String> names = new ArrayList<String>();


            //Add text file contents to the array list
            while((type = fr1.readLine()) != null)
            {
                names.add(type);
            }

            //Display array list
            System.out.println("Below is the list of animals with a comma and in the original order:");
            System.out.println(names);

            ****//Display information in descending order
            Collections.sort(names, Collections.reverseOrder());
            System.out.println(names);****


            //Convert array list to string and replace commas
            String fornames = names.toString().replace("," , " ");

            //Display altered information
            System.out.println("Here is the list with the commas removed:");
            System.out.println(fornames);                                               
        }

        catch(FileNotFoundException e)
        {
        System.out.println("Please utilize the file named myImport.txt please!");
        }

        catch(IOException e)
        {

        }
    }
}
import java.io.*;
导入java.util.*;
公共级ioStafford{
公共静态void revOrder(字符串x[]
{
}
公共静态void main(字符串参数[])
{
try(BufferedReader fr1=newbufferedreader(newfilereader(“myImport.txt”))
{
//如果反向输出不存在,请为其创建新文件
文件f1=新文件(“myOutput.txt”);
//创建新文件的代码
如果(!f1.exists())
{
f1.createNewFile();
}
//初始化字符串变量
字符串类型;
//用于保存文本文件的数组列表
ArrayList name=新的ArrayList();
//将文本文件内容添加到数组列表
而((type=fr1.readLine())!=null)
{
名称。添加(类型);
}
//显示数组列表

System.out.println(“下面是用逗号按原始顺序排列的动物列表:”); System.out.println(名称); ****//按降序显示信息 Collections.sort(名称,Collections.reverseOrder()); System.out.println(名称)**** //将数组列表转换为字符串并替换逗号 字符串fornames=names.toString().replace(“,”,”); //显示更改的信息 System.out.println(“这是删除逗号的列表:”; System.out.println(fornames); } catch(filenotfounde异常) { System.out.println(“请使用名为myImport.txt的文件!”); } 捕获(IOE异常) { } } }
在一个合适的地方试试这个

Collections.sort(names, Collections.reverseOrder());
:->

Collections.sort(名称、新比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回o2.compareTo(o1);
}
});

你能提供一个简短的例子(可能是4只动物)说明代码产生了什么吗?我认为排序没有问题。显示
名称
包含的内容。myImport.txt包含哪些内容?程序的输出是什么?我的猜测是,它只包含一行,其中包含了几种动物的名字,因此您的列表中只有一个字符串,因为代码逐行读取文件并将整行放入列表中。我很抱歉。文本文件包含猫、狗、熊、猴子。但你可以用任何东西来代替它。重要的部分是更改由逗号分隔的字符串值列表的顺序。下面是带有逗号的动物列表,其原始顺序为:[猫、猴子、狗、狮子、熊][猫、猴子、狗、狮子、熊]以下是删除逗号的列表:[猫、猴子、狗、狮子、熊]现在我看到了使用的比较器。我的问题是,您是否必须使用集合排序的比较器?因为我现在的印象是他们是相互独立的。朋友,你试过上面的方法吗。我相信它会起作用,我已经测试过了。是的,我们需要给集合提供comparator.sort(list,comparator)作为参数,如果我们必须对对象列表进行排序的话。如果我们想按自己的方式排序,这个代码不起作用。此外,我并不是真的在寻找一个直接的答案,我仍然想学习,坦率地说,我不打算只是复制和粘贴代码来实现这一点。谢谢你的帮助
    Collections.sort(names, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            return o2.compareTo(o1);
        }
    });