java - 对整数堆栈的数组列表进行排序时遇到问题

目前,我正在尝试创建一种方法来为我的作业对整数字典堆栈的数组列表进行排序。目前这是我所拥有的,但是,现在我面临一个问题。


Collections.sort(arrayList, new Comparator<Stack<Integer>>(){

            public int compare(Stack<Integer> list1, Stack<Integer> list2){

                int result = 0;

                for (int i = 0; i <= list1.size() - 1 && result == 0; i++)

                {

                    result = list2.get(i).compareTo(list1.get(i));

                }

                return result;

            }

        });

当我尝试输入[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 3], [1, 1, 1, 3, 1], [1, 1, 3, 1, 1], [1, 3, 1, 1, 1], [3, 1, 1, 1, 1], [1, 1, 3, 3], [1, 3, 1, 3], [1, 3, 3, 1], [3, 1, 1, 3], [3, 1, 3, 1], [3, 3, 1, 1], [1, 4, 4], [4, 1, 4], [4, 4, 1], [3, 3, 3]]作为整数堆栈的数组列表并设法得到这个结果时:


输出: [[4, 4, 1], [4, 1, 4], [3, 3, 3], [3, 3, 1, 1], [3, 1, 3, 1], [3, 1, 1, 3], [3, 1, 1, 1, 1], [1, 4, 4], [1, 3, 3, 1], [1, 3, 1, 3], [1, 3, 1, 1, 1], [1, 1, 3, 3], [1, 1, 3, 1, 1], [1, 1, 1, 3, 1], [1, 1, 1, 1, 3], [1, 1, 1, 1, 1, 1]]


正如你所看到的,这不是字典式的,这就是我的目标


预期输出: [[1, 4, 4], [3, 3, 3], [4, 1, 4], [4, 4, 1], [1, 1, 3, 3], [1, 3, 1, 3], [1, 3, 3, 1], [3, 1, 1, 3], [3, 1, 3, 1], [3, 3, 1, 1], [1, 1, 1, 1, 3], [1, 1, 1, 3, 1], [1, 1, 3, 1, 1], [1, 3, 1, 1, 1], [3, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]


这是问题,我哪里弄错了?.compareTo 是如何工作的?


编辑:


显然,我的教授对字典序的含义产生了误解。但是,预期的输出是我教授想要的输出


精慕HU
浏览 205回答 3
3回答

皈依舞

您需要首先检查其中一个堆栈是否比另一个短。如果不是,您需要比较对 的元素,list1而list2不是相反。Collections.sort(arrayList, new Comparator<Stack<Integer>>(){&nbsp; public int compare(Stack<Integer> list1, Stack<Integer> list2){&nbsp; &nbsp; &nbsp; int result = Integer.compare(list1.size(), list2.size());&nbsp; &nbsp; &nbsp; for (int i = 0; result == 0 && i < list1.size(); i++)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result = Integer.compare(list1.get(i), list2.get(i));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return result;&nbsp; }});

杨魅力

如果我们的字典序概念是相同的,那么问题似乎在于您使用的是倒置的 compareTo(list2 compareTo list1 而不是 list1 compareTo list2)。经过一些修改后,您的代码将如下所示:Collections.sort(arrayList, new Comparator<Stack<Integer>>() {&nbsp; &nbsp; public int compare(Stack<Integer> list1, Stack<Integer> list2) {&nbsp; &nbsp; &nbsp; &nbsp; int result = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <= list1.size() - 1 && result == 0; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list2.size()-1 < i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = list1.get(i).compareTo(list2.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }});此代码将产生以下结果:[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 3], [1, 1, 1, 3, 1], [1, 1, 3, 1, 1], [1, 1, 3, 3], [1, 3, 1, 1, 1], [1, 3, 1, 3], [1, 3, 3, 1], [1, 4, 4], [3, 1, 1, 1, 1], [3, 1, 1, 3], [3, 1, 3, 1], [3, 3, 1, 1], [3, 3, 3], [4, 1, 4], [4, 4, 1]],尽管您的预期结果我理解为按字典顺序排列。在if (list2.size()-1 < i)由于list2中比list1的较小for循环内防止IndexOutOfBoundsException异常。

素胚勾勒不出你

由于这是一项任务,我会将您放在我认为正确的方向上,但我不会为您解决编程问题:compare()是在接口Comparator<T>中定义的一个方法,它应该-1, 0, or 1根据作为参数传入的第一个对象小于、等于还是大于作为参数传入的第二个对象而返回。预期的对象是T您在声明类时定义的类型。在compare()您编写的方法中,您必须将要实现的任何比较方法解析为-1, a 0, or a 1.如何获得这些值取决于您如何评估一个T类型的对象是否小于、等于或大于相同类型的其他对象。另一方面,Array.sort()将使用Comparatoras fit 在数组的两个元素之间进行比较,并作为最终结果返回 sorted&nbsp;Array。如果我理解您的说明,则array [1,1,1]出于订购目的的 , 应解释为String "111";因此,在编程方面Comparator,会有什么比较(与T)会arrays的integers。并比较两个arrays的integers,每个阵列中的元素应被提取并粘在一起形成String。一旦你拥有两者Strings,你就可以看到它们如何相互比较。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java