Java队列和多维数组

时间:2021-12-10 21:36:14

First of all, this is my code (just started learning java):

首先,这是我的代码(刚开始学习java):

Queue<String> qe = new LinkedList<String>();

qe.add("b");
qe.add("a");
qe.add("c");
qe.add("d");
qe.add("e");

My question:

  1. Is it possible to add element to the queue with two values, like:

    是否可以使用两个值将元素添加到队列中,例如:

    qe.add("a","1"); // where 1 is integer

    qe.add( “一”, “1”); //其中1是整数

So, that I know element "a" have value 1. If I want to add a number let say "2" to element a, I will have like a => 3.

所以,我知道元素“a”有值1.如果我想添加一个数字,让我们说“2”到元素a,我会有一个=> 3。

If this cant be done, what else in java classes that can handle this? I tried to use multi-dimention array, but its kinda hard to do the queue, like pop, push etc. (Maybe I am wrong)

如果不能这样做,java类中还能处理这个问题吗?我试图使用多维数组,但它有点难以排队,如pop,push等(也许我错了)

  1. How to call specific element in the queue? Like, call element a, to check its value.
  2. 如何调用队列中的特定元素?比如,调用元素a来检查它的值。

[Note]

Please don't give me links that ask me to read java docs. I was reading, and I still dont get it. The reason why I ask here is because, I know I can find the answer faster and easier.

请不要给我链接,要求我阅读java文档。我正在读书,我仍然没有得到它。我在这里问的原因是因为,我知道我可以更快更容易地找到答案。

3 个解决方案

#1


1  

I think you're asking for a dictionary type in Java.

我想你要求Java中的字典类型。

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);

You can then access them by key - in this case the String you choose as the key.

然后,您可以通过键访问它们 - 在这种情况下,您选择作为键的String。

int value = map.get("a");

Value in this case will return 1.

在这种情况下,值将返回1。

Is that what you want?

那是你要的吗?

#2


2  

You'd want to combine a Queue<K> with a Map<K,V>:

您想要将队列 与Map 组合: ,v>

  • Put the keys (e.g. "a", "b") into the Queue<K>
  • 将密钥(例如“a”,“b”)放入Queue

  • Assign the mapping of the keys to values (e.g. "a"=>3) in the Map<K,V>
  • 在Map 中将键的映射分配给值(例如“a”=> 3) ,v>

#3


1  

You want to use a HashMap instead of LinkedList. HashMap is a dictionary-like structure that allows you to create associations, for instance a=>1.

您想使用HashMap而不是LinkedList。 HashMap是一个类似字典的结构,允许您创建关联,例如a => 1。

Check out JavaDocs for HashMap to get a grasp how to use it:-).

查看JavaDocs for HashMap以了解如何使用它:-)。

#1


1  

I think you're asking for a dictionary type in Java.

我想你要求Java中的字典类型。

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);

You can then access them by key - in this case the String you choose as the key.

然后,您可以通过键访问它们 - 在这种情况下,您选择作为键的String。

int value = map.get("a");

Value in this case will return 1.

在这种情况下,值将返回1。

Is that what you want?

那是你要的吗?

#2


2  

You'd want to combine a Queue<K> with a Map<K,V>:

您想要将队列 与Map 组合: ,v>

  • Put the keys (e.g. "a", "b") into the Queue<K>
  • 将密钥(例如“a”,“b”)放入Queue

  • Assign the mapping of the keys to values (e.g. "a"=>3) in the Map<K,V>
  • 在Map 中将键的映射分配给值(例如“a”=> 3) ,v>

#3


1  

You want to use a HashMap instead of LinkedList. HashMap is a dictionary-like structure that allows you to create associations, for instance a=>1.

您想使用HashMap而不是LinkedList。 HashMap是一个类似字典的结构,允许您创建关联,例如a => 1。

Check out JavaDocs for HashMap to get a grasp how to use it:-).

查看JavaDocs for HashMap以了解如何使用它:-)。