什么是设置<组件>意味着?组件、LT、gt

2023-09-04 03:44:20 作者:〆、我们却要爱到丶离开。

我是一种新的以Android和我得把两个PCB之间蓝牙连接。我看到了API导游一行code和我还没有弄明白是什么意思。我不知道是否有人能帮助我。

I'm kind of new to Android and I have to make a Bluetooth connection between two PCBs. I saw a line of code in API guides and I still haven't figure out what it means. I wonder if someone can help me.

下面是code:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

我不明白的是设置&LT; BluetoothDevice类&GT;!

为什么他们把与的东西&LT;&gt;中。我也见过比 ArrayAdapter&LT;字符串&GT; 。什么这些元素呢?

Why does they put something between "< >". I've also seen than in ArrayAdapter<String>. What these elements do?

推荐答案

这使得设置 A的通用集。当你声明的:

That makes the Set a Generic set. When you declare this :

Set<BluetoothDevice> pairedDevices

意味着设置对象应包含类型的对象 BluetoothDevice类。使用泛型集合一般建议,因为你获得类型安全的直接好处。

means the Set object should contain only objects of type BluetoothDevice. Using generic collections is generally recommended, because you gain the immediate benefit of type safety.

Java集合框架被设计成处理任何类型的对象。在的Java 1.4 的早期和他们使用的java.lang.Object 作为任何对象的类型添加到集合。你必须显式转换对象所需的类型,当你使用它们,否则你会得到编译时错误。

The Java Collections Framework was designed to handle objects of any type. In Java 1.4 and earlier they used java.lang.Object as the type for any object added to the collection. You had to explicitly cast the objects to the desired type when you used them or else you would get compile-time errors.

Java泛型,在出台的的Java 5 的,提供更强的类型安全。泛型允许类型作为参数传递给类,接口和方法声明。例如:

Java Generics, introduced in Java 5, provide stronger type safety. Generics allow types to be passed as parameters to class, interface, and method declarations. For example:

Set<BluetoothDevice> pairedDevices

&LT; BluetoothDevice类&GT; 在这个例子是一个类型参数。随着类型参数,编译器将确保我们使用集合只兼容类型的对象。 另一个好处是,我们将不需要转换我们从集合获得的对象。现在在编译时检测到的对象类型的错误,而不是抛出铸造的运行期异常。

The <BluetoothDevice> in this example is a type parameter. With the type parameter, the compiler ensures that we use the collection with objects of a compatible type only. Another benefit is that we won’t need to cast the objects we get from the collection. Object type errors are now detected at compile time, rather than throwing casting exceptions at runtime.

推荐阅读:

甲骨文公司的仿制药指南 使用和J2SE 5.0 泛型编程 泛型在Java中 - 维基 Java理论与实践:泛型陷阱 Java泛型常见问题解答 协方差和逆变 Oracle's tutorial on Generics Using and Programming Generics in J2SE 5.0 Generics in Java - Wiki Java theory and practice: Generics gotchas Java Generics FAQs Covariance and contravariance