【问题标题】:Queueing variables in JAVA and return the variable one by one instead of value.在JAVA中对变量进行排队,并一一返回变量而不是值。
【发布时间】:2014-05-06 14:13:06
【问题描述】:

我是 JAVA 新手。我正在研究优先队列。 假设我最终得到了这 4 个具有各自值的变量, a1=2、a2=-4、a3=-8 和 a4=10; 现在我想按升序对它们进行排序。通过排序或优先队列,我知道如何按升序获取值,但是如何在 JAVA 中获取 变量名? 就像在这种情况下,它会像这样[a3,a2,a1,a10]对变量名进行排序。然后我会拿 a3、a2 等等,然后把它们放到其他任务中。我对获取变量名而不是值感兴趣。有可能吗?

【问题讨论】:

  • 看起来你想要一个数组int[] a,它可以让你写a[0]a[1]a[2]等。

标签: java sorting queue priority-queue


【解决方案1】:

如何在 JAVA 中获取变量名?

不,这是不可能的。您可以做的最接近的事情是将变量及其值存储在 Map<String, Object> 中,并将变量的 name 分配为键:

//maintain order of the elements by the key
Map<String, Integer> mapVariables = new TreeMap<String, Integer>();
mapVariables.put("a1", a1);
mapVariables.put("a2", a2);
mapVariables.put("a3", a3);
mapVariables.put("a4", a4);

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2023-04-01
    • 2012-09-08
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多