类似于 Java 中的 C# .NET Generic List

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

Something similar to C# .NET Generic List in java

c#java.net

提问by RollRoll

How would be a c# .net generic list in java?

java中的ac#.net泛型列表如何?

somthing like that:

类似的东西:

public class ClientList : List<Client> { }


the answer from Nikil was perfect, I just want to add to whoever wants to create a class from the List:

Nikil 的回答是完美的,我只想添加到想要从 List 创建一个类的任何人:

public class ClientList extends ArrayList<Client>

回答by Nikhil Agrawal

Java's List interface (java.util.List) can be generified. In other words, instances of List can be given a type, so only instances of that type can be inserted and read from that List. Here is an example:

Java 的 List 接口(java.util.List)可以被泛型化。换句话说,List 的实例可以被赋予一个类型,因此只有该类型的实例才能从该 List 中插入和读取。下面是一个例子:

List<String> list = new ArrayList<String>();

This list is now targeted at only String instances, meaning only String instances can be put into this list. If you try to put something else into this List, the compiler will complain.

此列表现在仅针对 String 实例,这意味着只能将 String 实例放入此列表中。如果您尝试将其他内容放入此 List 中,编译器会抱怨。

The generic type checks only exists at compile time. At runtime it is possible to tweak your code so that a String List has other objects that String's inserted. This is a bad idea, though.

泛型类型检查只存在于编译时。在运行时,可以调整您的代码,以便字符串列表具有插入字符串的其他对象。不过,这是一个坏主意。

Accessing a Generic List

访问通用列表

You can get and insert the elements of a generic List like this:

您可以像这样获取和插入通用列表的元素:

List<String> list = new ArrayList<String>();

String string1 = "a string";
list.add(string1);

String string2 = list.get(0);

Notice how it is not necessary to cast the object obtained from the List.get() method call, as is normally necessary. The compiler knows that this List can only contain String instances, so casts are not necessary.

请注意,没有必要强制转换从 List.get() 方法调用获得的对象,这通常是必要的。编译器知道此 List 只能包含 String 实例,因此不需要强制转换。

Iterating a Generic List

迭代通用列表

You can iterate a generic List using an iterator, like this:

您可以使用迭代器迭代通用列表,如下所示:

List<String> list = new ArrayList<String>();

Iterator<String> iterator = list.iterator();

while(iterator.hasNext()){
    String aString = iterator.next();
}

Notice how it is not necessary to cast the object returned from the iterator.next() next call. Because the List is generified (has a type), the compiler knows that it contains String instances. Therefore it is not necessary to cast the objects obtained from it, even if it comes from its Iterator.

请注意如何不必强制转换从 iterator.next() 下一次调用返回的对象。因为 List 是泛型的(有一个类型),所以编译器知道它包含 String 实例。因此没有必要转换从它获得的对象,即使它来自它的 Iterator。

You can also use the new for-loop, like this:

您还可以使用新的 for 循环,如下所示:

List<String> list = new ArrayList<String>();

for(String aString : list) {
    System.out.println(aString);
}

Notice how a String variable is declared inside the parantheses of the for-loop. For each iteration (each element in the List) this variable contains the current element (current String).

注意在 for 循环的括号内如何声明 String 变量。对于每次迭代(列表中的每个元素),此变量包含当前元素(当前字符串)。