Tag: 排序

同时排序两个数组

我现在正在学习和理解Java,在练习数组时我有一个疑问。 我编写了以下代码作为示例: class example { public static void main(String args[]) { String a[] = new String[] {“Sam”, “Claudia”, “Josh”, “Toby”, “Donna”}; int b[] = new int[] {1, 2, 3, 4, 5}; for(int n=0;n<5;n++) { System.out.print (a[n] + "…"); System.out.println (b[n]); } System.out.println (" "); java.util.Arrays.sort(a); for(int n=0;n<5;n++) { System.out.print (a[n] + "…"); System.out.println (b[n]); } […]

Java代码审查:将已排序的列表合并为单个排序列表

我想将排序列表合并到一个列表中。 这个解决方案怎么样? 我相信它会在O(n)时间内运行。 任何明显的缺陷,效率低下或风格问题? 我真的不喜欢为“这是第一次迭代”设置标志的习惯用法,并使用它来确保“最低”具有默认值。 有更好的方法吗? public static <T extends Comparable> List merge(Set<List> lists) { List result = new ArrayList(); int totalSize = 0; // every element in the set for (List l : lists) { totalSize += l.size(); } boolean first; //awkward List lowest = lists.iterator().next(); // the list with the lowest item to […]

Arrays.sort()和Arrays.parallelSort()之间的区别

正在阅读这里提到的Java 8function。 无法理解parallelSort()确切作用。 有人可以解释sort()和parallelSort()之间的实际区别是什么?

MyClass无法强制转换为java.lang.Comparable:java.lang.ClassCastException

我正在做一个java项目,我遇到了这个问题,不知道如何解决它。 我项目中的类(简化): public class Item { private String itemID; private Integer price; public Integer getPrice() { return this.price; } } public class Store { private String storeID; private String address; } public class Stock { private Item item; private Store store; private Integer itemCount; public Integer getInventoryValue() { return this.item.getPrice() * this.itemCount; } } 然后我尝试排序Stock的ArrayList […]

如何使用Java对文本文件中的记录进行排序?

对txt文件中的数据进行了一些修改。 我已经尝试了建议的代码,但我没有成功地用这种格式在txt文件中再次写它。我尝试了collection.sort但是它用长线写了数据。 我的txt文件包含以下数据: Monday Jessica Run 20mins Alba Walk 20mins Amy Jogging 40mins Bobby Run 10mins Tuesday Mess Run 20mins Alba Walk 20mins Christy Jogging 40mins Bobby Run 10mins 如何按升序对这些数据进行排序,并在排序后将其再次存储在txt文件中? Monday Alba Walk 20mins Amy Jogging 40mins Bobby Run 10mins Jessica Run 20mins Tuesday Alba Walk 20mins Bobby Run 10 mins Christy Jogging 40mins Mess […]

Java:如何以相反的顺序对浮点数组进行排序?

我使用以下行以相反的顺序对浮点数组进行排序,但是我收到了错误消息,出了什么问题? float sortedData[]=new float[100]; … Arrays.sort(sortedData,Collections.reverseOrder()); 错误:找不到符号 symbol:方法sort(float [],java.util.Comparator)location:class java.util.Arrays Arrays.sort(sortedData,Collections.reverseOrder()); ================================================== ======================= 我很困惑因为在Jdk1.6 api中,我看到了这个:[Arrays] public static void sort( float [] a),它没有说:public static void sort( Float [] a)

如何在java中的目录中对文件进行排序?

这是我的代码,它的工作原理! 但我希望能够根据名称,大小,修改日期等对文件列表进行排序 import java.io.File; import org.apache.commons.io.FileUtils; public class StartingPoint { public static void main(String[] args) { File file = new File( “/home/t/lectures”); File[] files = file.listFiles(); for (File f : files) { System.out.println(“File : ” + f.getName() + ” [” + FileUtils.byteCountToDisplaySize(f.length()) + “]”); } } }

‘MergeSort算法’ – JAVA中更好的实现是什么?

我知道快速排序算法,但我只关心合并排序算法。 我在互联网上发现了两种类型的合并排序算法实现。 但是当我将它们与插入算法进行比较时,它们似乎效率较低,而且对于大量项目而言并不是这样。 Enter the number of elements you want to sort: 300000 Time spent to executing BubbleSort: 362123 milliseconds Time spent to executing Selection: 108285 milliseconds Time spent to executing Insertion: 18046 milliseconds Time spent to executing MergeSort: 35968 milliseconds Time spent to executing MergeSort2: 35823 milliseconds 是否有另一种方法来实现合并排序算法,使其比插入算法更有效? 看看我的代码…… package br.com.test.test1; import java.util.Random; import […]

使用自定义Comparator在Java中创建SortedMap

我想用Java自定义排序顺序创建一个TreeMap 。 作为字符串的排序键需要根据第二个字符进行排序。 值也是字符串。 示例地图: Za,FOO Ab,Bar

按长度排序字符串的ArrayList

我想按长度订购字符串的ArrayList,但不仅仅是按数字顺序。 比如说,列表包含以下单词: cucumber aeronomical bacon tea telescopic fantasmagorical 它们需要按长度差异排序为特殊字符串,例如: intelligent 所以最终的列表看起来像这样(括号中的差异): aeronomical (0) telescopic (1) fantasmagorical (3) – give priority to positive differences? doesn’t really matter cucumber (3) bacon (6) tea (8)