java Java中的元素是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36389666/
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
What's Element in Java?
提问by user1991275
In javadoc about ArrayList you can read about the method add(E e) where e is an element. What's the differense between elements and objects? I am interested in how the elements are different from objects in Java, not generics .
在关于 ArrayList 的 javadoc 中,您可以阅读有关 add(E e) 方法的信息,其中 e 是一个元素。元素和对象有什么区别?我对元素与 Java 中的对象有何不同感兴趣,而不是泛型。
回答by Yassin Hajaj
Elements are no different from Objects in Java.
元素与 Java 中的对象没有区别。
In real life though
虽然在现实生活中
It is a part of good practices used when using Java, but they all design Objects. Knowing that in Java, when writing classes using genericity, you may use the names you want like below
它是使用 Java 时使用的良好实践的一部分,但它们都是设计对象。知道在 Java 中,在使用泛型编写类时,您可以使用您想要的名称,如下所示
public class MyClass<IChooseTheNameIWant> {} // This is totally valid.
public class MyClass<T> {} // This is valid AND respects good practices.
It is better to follow the good practices to ensure durability/readability (for others) of your code. And this is what the Java language architects did when designing ArrayList
.
最好遵循良好实践以确保代码的持久性/可读性(对其他人而言)。这就是 Java 语言架构师在设计ArrayList
.
Generics : How They Work?
泛型:它们如何工作?
Type parameters, also known as type variables, are used as placeholders to indicate that a type will be assigned to the class at runtime. There may be one or more type parameters, and they can be utilized throughout the class, as needed. By convention, type parameters are a single uppercase letter, and the letter that is used indicates the type of parameter being defined. The following list contains the standard type parameters for each use case:
E: Element K: Key N: Number T: Type V: Value S, U, V, and so on: Second, third, and fourth types in a multiparameter situation
类型参数,也称为类型变量,用作占位符以指示将在运行时将类型分配给类。可能有一个或多个类型参数,并且可以根据需要在整个类中使用它们。按照惯例,类型参数是单个大写字母,使用的字母表示正在定义的参数类型。以下列表包含每个用例的标准类型参数:
E: Element K: Key N: Number T: Type V: Value S, U, V, and so on: Second, third, and fourth types in a multiparameter situation
回答by Joni
In general, an element is a part of a whole. For example, the number 4 is an element in 1,2,4,8
. The number 16 and the string elephant
are not.
一般来说,元素是整体的一部分。例如,数字 4 是 中的一个元素1,2,4,8
。数字 16 和字符串elephant
不是。
In Java, the elements of a list can be references to objects, or the special null
value that references no object.
在 Java 中,列表的元素可以是对对象的引用,也可以是null
不引用任何对象的特殊值。
回答by ΦXoc? ? Пepeúpa ツ
In that context Elementis a Generic Objectexample: you can not do List<int>
instead mus use the class Integer and do List<Integer>
在那种情况下Element是一个通用对象示例:您不能这样做,List<int>
而必须使用 Integer 类并执行List<Integer>