开发者

Explanation of how List class works

开发者 https://www.devze.com 2023-04-12 09:22 出处:网络
I have a question about the List class. From what I understand List is a class deriving from the Collection class. My problem is that when I read examples on the internet, it says for example List <

I have a question about the List class. From what I understand List is a class deriving from the Collection class. My problem is that when I read examples on the internet, it says for example List < Entry > entries;

I'm having slight difficulties understand what that actually means? Entry is supposed to be another class but I do not understand how it works. I read it as, create list of type Entry and store it in entries, is that correct? Just to clarify my question, what confuses me most is the Entry part of it, if it's anot开发者_运维问答her class how does that make the list function? IS it the object created in the class Entry which describes the Type of list it is? An example would be great.

I apologize if I have not been clear enough, any help much appreciated. I'm talking about Java in this particular case.


It's called Generics. The Entry is the type argument. List<Entry> means "a list which contains Entry objects".

Worked example:

Entry entry1 = new Entry();
Entry entry2 = new Entry();
Entry entry3 = new Entry();
List<Entry> entryList = new ArrayList<Entry>();

list.add(entry1);
list.add(entry2);
list.add(entry3);

And you cannot do this:

Boat wrongTypeObject = new Boat();
list.add(wrongTypeObject);

because the object wrongTypeObject is not an Entry.

Good article here:

http://en.wikipedia.org/wiki/Generics_in_Java


It must be read "List of Entry". It describes what kind of object can go into the list. Read the generics tutorial to learn more.


There's no such thing as a generic collection in Java. The list wants to know what you're going to put inside it. For example, List is a list containing Strings. List is a list containing Integers.

You should note that you can't store primitives (ints, floats, doubles, etc.) inside a collection. You must box them. (int -> Integer, float -> Float, double -> Double).


An Entry is an object used to store the contents of a Map-derived class, so you may well encounter the occasional List<Entry> objects. However, a list can hold any kind of object, such as Strings, Integers, or even other Lists:

List<Integer> intList = new ArrayList<Integer>();  //Create a new list of integers
List<String> strList = new ArrayList<String>(); //You can't actually create a List.  Use one of the derived classes like ArrayList instead

strList.add("A string!");  // Now the list has one String in it
strList.add(3);  //You can't do this because strList can only hold Strings

ArrayList< ArrayList<String> > strMatrix = new ArrayList< ArrayList<String> >()

That last line creates a list of lists, which is commonly used to store a 2-D array of objects.

Don't worry too much about Entrys until you need them, and then you can consult the docs to see how to get data out of them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号