【发布时间】:2021-11-23 10:03:49
【问题描述】:
当Hashmap没有排序或排序时,这个程序是如何对数组进行排序的?
此程序正在打印来自两个不同排序数组的排序映射
class A {
// Function to merge arrays
static void mergeArrays(int a[], int b[], int n, int m)
{
// Declaring a map.
// using map as a inbuilt tool
// to store elements in sorted order.
Map<Integer,String> mp = new HashMap<Integer,String>();
// Inserting values to a map.
for(int i = 0; i < n; i++)
{
mp.put(a[i], "a");
}
for(int i = 0;i < m;i++)
{
mp.put(b[i], "a");
}
// Printing keys of the map.
for (Entry<Integer, String> me : mp.entrySet())
{
System.out.print(me.getKey() + " ");
}
}
public static void main (String[] args)
{
int a[] = {1,3,5,7}, b[] = {2, 4, 6, 8};
int size = a.length;
int size1 = b.length;
mergeArrays(a, b, size, size1);
}
}
【问题讨论】:
-
地图有 a 顺序,但该顺序是不可依赖的 - 您可能观察到的任何顺序纯属巧合。因此,“这个程序如何对数组进行排序 [...]”的答案是:不是。