Java 不同类型的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18291024/
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
Different types of lists
提问by Sam
Hello I am studying Java and I am just confused about a question that keeps coming up in my exams.
您好,我正在学习 Java,我只是对考试中不断出现的问题感到困惑。
We are told to use a class for our collections.
我们被告知要为我们的集合使用一个类。
Usually in my main class I just make an arraylist of the type I need like bank accounts or whatever. But I think we are supposed to make a class for all our data. I tried this and made them static and have the class abstract because I don't need to make copies of the class itself.
通常在我的主课中,我只制作一个我需要的类型的数组列表,比如银行账户或其他什么。但我认为我们应该为所有数据创建一个类。我尝试了这个并使它们成为静态并具有抽象类,因为我不需要制作类本身的副本。
What I want to know is why don't people just make a list type that can do everything an arraylist and list can do in one, why is there different types? What are the benifits of having different types? Ps does a collection just mean a class for all your data?
我想知道的是,为什么人们不制作一个列表类型,它可以完成一个数组列表和列表可以做的所有事情,为什么会有不同的类型?拥有不同类型有什么好处?Ps 集合是否意味着所有数据的类?
回答by Mingyu
Every List has its own usage:
每个 List 都有自己的用法:
ArrayList behaves like a Resizable-array
LinkedList could be used as a FIFO queue
Stack obviously is a stack (LIFO)
ArrayList 的行为类似于可调整大小的数组
LinkedList 可以用作 FIFO 队列
堆栈显然是堆栈(LIFO)
Here's the javadoc for list:
这是列表的javadoc:
http://docs.oracle.com/javase/6/docs/api/java/util/List.html
http://docs.oracle.com/javase/6/docs/api/java/util/List.html