[英]Java sorted array output into linked list
is there a way to sort an array and store the result in a linked list with the original array positions referenced by the list? 有没有一种方法可以对数组进行排序并将结果存储在链表中,该链表具有该列表引用的原始数组位置?
so 所以
0 bbb 1 aaa 2 ccc 0 bbb 1 aaa 2 ccc
would become a linked list 将成为链表
1 aaa 0 bbb 2 ccc 1 aaa 0 bbb 2 ccc
Thanks 谢谢
MyClass[] array = new MyClass[n];
// fill the array
Arrays.sort(array);
List<MyClass> list = new LinkedList<Class>(Arrays.asList(array));
You can just use a TreeMap
: 您可以只使用TreeMap
:
final SortedMap<String, Integer> map = new TreeMap<String, Integer>(array.length);
for (int index = 0; index < array.length; i++)
map.put(array[index], index);
The keys are therefore the strings, in order, and the positions in the original aray are the values. 因此,键是按顺序的字符串,原始数组中的位置是值。 But storing a String and its origin position requires a dedicated structure. 但是存储字符串,其原点位置需要一个专用的结构。 Or maybe the Map.Entry<String, Integer>
entries for the map are enough for your needs. 或者,地图的Map.Entry<String, Integer>
条目足以满足您的需求。 It depends on what you want to do. 这取决于您想做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.