java 如何在java中制作二维LinkedList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14573190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make two dimensional LinkedList in java?
提问by iAbdul
for example:
例如:
public static LinkedList<String, Double> ll = new LinkedList<String, Double>;
回答by Kent
from your question, I think (not 100% sure) you are looking for
java.util.LinkedHashMap<K, V>
从你的问题来看,我认为(不是 100% 确定)你正在寻找
java.util.LinkedHashMap<K, V>
in your case, it would be LinkedHashMap<String, Double>
在你的情况下,这将是 LinkedHashMap<String, Double>
from java doc:
来自 java 文档:
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.
Map接口的哈希表和链表实现,迭代顺序可预测。此实现与 HashMap 的不同之处在于它维护一个双向链表,贯穿其所有条目。
if you do want to get element by list.get(5)
, you could :
如果您确实想通过 获取元素list.get(5)
,您可以:
LinkedList<Entry<String, Double>>
so you can get Entry element by Entry entry = list.get(5)
, then entry.getKey()
gives you the STring, and entry.getValue()
gives you the Double.
因此您可以通过 获取 Entry 元素Entry entry = list.get(5)
,然后entry.getKey()
为您提供字符串,并entry.getValue()
为您提供双精度值。
回答by jlordo
Reading all your comments, I suggest you do something like this:
阅读您所有的评论,我建议您执行以下操作:
public class StringAndDouble {
private String str;
private double dbl;
// add constructor
// add getters, setters and other methods as needed.
// override equals() and hashCode()
}
Now you can use:
现在您可以使用:
List<StringAndDouble> list = new LinkedList<>(); // or
List<StringAndDouble> list = new ArrayList<>(); // better in most cases
Now you can access your objects by index.
现在您可以通过索引访问您的对象。
This answer creates a new class, to fit your needs. The class has two fields, one String
, one double
. This doesn't make the class two dimensional. I think you have a misunderstanding there. When there are n
dimensions, you need n
indexes to access an element. You were talking of accessing by index, so I assume you're looking for a one dimensional list holding the objects, that have more than one field.
这个答案创建了一个新类,以满足您的需求。该类有两个字段,一String
,一double
。这不会使类成为二维的。我想你那里有误解。当有n
维度时,您需要n
索引来访问元素。您说的是按索引访问,所以我假设您正在寻找一个包含多个字段的对象的一维列表。
回答by user949300
Since OP in a comment to @Kent says he wants to be able to get items by index...
由于 OP 在对@Kent 的评论中说他希望能够通过索引获取项目......
Note that a LinkedList
(and LinkedHashMap
) are inefficientat that. He may prefer an ArrayList
. So I would suggest that his "2D" implementation be a
请注意, a LinkedList
(和LinkedHashMap
)在这方面效率低下。他可能更喜欢ArrayList
. 所以我建议他的“2D”实现是一个
ArrayList<Map.Entry<String, Double>>
which will efficiently support a get by index.
这将有效地支持按索引获取。
As for the normal get(String key)
, you'd have to do a linear search of all the entries, which would be inefficient.
至于 normal get(String key)
,您必须对所有条目进行线性搜索,这将是低效的。
So, you have a decision: which way of accessing (by a key or by an index) is more important?
所以,您有一个决定:哪种访问方式(通过键或通过索引)更重要?
回答by Lornikof
You can actually use Linked Lists within eachother... For Example:
您实际上可以在彼此之间使用链表......例如:
public LinkedList<LinkedList<Integer>> twoDimLinkedList = new LinkedList<LinkedList<Integer>>();
Then:
然后:
////////////////
int value = twoDimLinkedList.get(3).get(4);
/////////////////
or (If you were planning on using it for iterative purposes):
或(如果您打算将其用于迭代目的):
/////////////////
for (int i = 0; i < twoDimLinkedList.size(); i++) {
LinkedList<Integer> twoDimLinkedListRow = new LinkedList<Integer>();
for (int m = 0; m < twoDimLinkedList.get(i).size(); m++) {
twoDimLinkedListRow.add(value);
}
twoDimLinkedList.add(twoDimLinkedListRow);
}
////////////////
回答by Nikola Mitev
Do you mean like this?
你的意思是这样吗?
HashMap<String, Double> hm = new HashMap<String, Double>();