在 Java 中声明 LinkedList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9844236/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:28:28  来源:igfitidea点击:

Declaring a LinkedList in Java

javalistcollectionslinked-list

提问by Márcio Duarte

I always learn when we declare a collection we should do, Interface ob = new Class(), if i want to use for example a LinkedList i'll do List ob = new LinkedList(), but then i can't have access to all methods from LinkedList.. Isn't LinkedList ob = new LinkedList()100% correct?

当我们声明一个我们应该做的集合时,我总是学习Interface ob = new Class(),如果我想使用例如一个 LinkedList 我会这样做List ob = new LinkedList(),但是我无法访问 LinkedList 中的所有方法..不是LinkedList ob = new LinkedList()100% 正确吗?

回答by Jon Skeet

Isn't LinkedList ob = new LinkedList() 100% correct?

LinkedList ob = new LinkedList() 不是 100% 正确吗?

Well I'd suggest using the generic form, but sure - if you want to use functionality which is specificto LinkedList, you need to declare the variable accordingly.

好吧,我建议使用通用形式,但可以肯定 - 如果您想使用特定于 的功能,则LinkedList需要相应地声明变量。

You might want to check whether the Deque<E>or Queue<E>interfaces have what you want though. If they do, use those in-keeping with the idea of describing what you need rather than what implementation you'll use.

您可能想检查Deque<E>Queue<E>接口是否具有您想要的东西。如果他们这样做,请使用那些与描述您需要什么而不是您将使用什么实现的想法保持一致的内容。

回答by ruakh

Yes,

是的,

LinkedList<...> items = new LinkedList<...>();

is perfectly correct if you know that itemswill depend on methods of LinkedList<T>that are not captured in the List<T>interface.

如果您知道这items将取决于界面LinkedList<T>中未捕获的方法,则是完全正确的List<T>

回答by Makoto

Not exactly 100% correct.

不完全是 100% 正确。

A preferred way to declare any collection is to include the data type it's holding. So, for your example, it'd be LinkedList<Integer> ob = new LinkedList<Integer>();.

声明任何集合的首选方法是包含它所持有的数据类型。所以,对于你的例子,它会是LinkedList<Integer> ob = new LinkedList<Integer>();.

回答by Jeffrey Blattman

the rule "always code to interfaces" must be taken with some flexibility. what you are suggesting is fine, and as you came to the conclusion, the only option.

“始终对接口进行编码”的规则必须具有一定的灵活性。你的建议很好,当你得出结论时,这是唯一的选择。

as a side note, coding to concrete classes like this is faster is most JVMs. deciding whether the performance is worth breaking the rule is the hard thing to decide.

附带说明一下,大多数 JVM 对像这样的具体类进行编码会更快。决定表演是否值得打破规则是一件很难决定的事情。

回答by Affe

If you actually have a need to use methods that are not on the List interface, there is certainly nothing wrong with using LinkedList's API. The general rule of programming to the List interface recognizes that 1) it's pretty rare to need those methods, and 2) in most people's experience, it's way more likely that I discover I need to sort the list and/or use a lot of random access, and decide to switch to an ArrayList, than it is I need one of the methods only LinkedList has.

如果您确实需要使用不在 List 接口上的方法,那么使用 LinkedList 的 API 肯定没有错。List 接口编程的一般规则认识到 1) 很少需要这些方法,2) 在大多数人的经验中,我更有可能发现我需要对列表进行排序和/或使用大量随机访问,并决定切换到 ArrayList,而不是我需要只有 LinkedList 具有的方法之一。

It may be also that you could be programming to the Queue interface, if you find List isn't giving you what you need.

如果您发现 List 没有提供您需要的东西,那么您也可能正在对 Queue 接口进行编程。

回答by Eugene Feingold

LinkedList is a generic. You should be doing:

LinkedList 是一个泛型。你应该这样做:

LinkedList<String> linkedList = new LinkedList<String>();

(or whatever else you need to store in there instead of String)

(或者你需要在那里存储的任何其他东西而不是字符串)

回答by michele b

You should always try to keep the declaration at the highest level possible, meaning that you should stop at the highest level that provides all the functionality that you need: if Listmethods are not enough, you're perfectly fine with your LinkedListdeclaration.

您应该始终尝试将声明保持在可能的最高级别,这意味着您应该停留在提供您需要的所有功能的最高级别:如果List方法不够,那么您的LinkedList声明完全没问题。

回答by aretai

Actually it would be better if it would be parametrized as both are raw types.

实际上,如果将其参数化会更好,因为两者都是原始类型。

回答by Punith Raj

Nope.. This would be wrong, at the later stages if he wants to change his implementation from linked list to any other implementation of list type he will go wrong... So better to use the interface level declaration.

不.. 这将是错误的,在后期阶段,如果他想将他的实现从链表更改为列表类型的任何其他实现,他会出错......所以最好使用接口级别声明。

回答by Arjun K P

I won't always suggest you to use generics ..... Coz sometimes you may need to wrap different objects as here....

我不会总是建议你使用泛型......因为有时你可能需要像这里一样包装不同的对象......

          String str="a string";
          boolean status=false;

          LinkedList ll = new LinkedList();
          ll.add(str);
          ll.add(status);

In some situations like case of RMI, u can only send serialized data.....and suppose you want to send a class object(which is unserialized).......There you can wrap the members of the class(primitives) in a LinkedList and pass that object as a whole.......not worrying about the huge number of arguments...... Consider for eg:

在某些情况下,如 RMI 的情况,你只能发送序列化数据......并且假设你想发送一个类对象(这是非序列化的)......在那里你可以包装类的成员(原语)在一个 LinkedList 中并将该对象作为一个整体传递.......不用担心大量的参数......考虑例如:

       public Class DataHouse
       {
             public int a;
             public String str;
             .
             .
             .
       }

Now Somewhere u need to pass the objects.... You can do the following....

现在某处你需要传递对象......你可以执行以下操作......

             DataHouse dh =new DataHouse();
             LinkedList ll = new LinkedList();
             ll.add(dh.a);
             ll.add(dh.str);

             // Now the content is serialized and can pass it as a capsuled data......