Java 中 CompositeName get() 方法及示例

2025 年 1 月 6 日 | 阅读 2 分钟

javax.naming.CompositeName 是一个包含 get() 方法的类。要获取此复合名称对象的组件,请使用 CompositeName 类。通过作为参数提供的位置,可以从复合名称对象中获取该位置的组件。

语法

参数

此方法接受要检索的组件的 0 基索引 posn。它需要介于 0 和 size() 之间。

返回值

此方法返回索引 posn 处的元素。

Exception

如果 posn 超出给定范围,此函数将抛出 ArrayIndexOutOfBoundsException 异常。

示例 1

提供的 Java 程序演示了如何使用 javax.naming 包中的 CompositeName 类来管理和获取复合名称的组件。应用程序使用复合名称字符串 "pq/yaz/abz/xe/klm" 初始化 CompositeName 对象,该字符串是分隔符为斜杠的组件序列。然后,它在索引位置 1 和 4 处使用 get() 方法从复合名称中检索并打印特定组件;这些组件分别是 "yaz" 和 "klm"。

实施

文件名: CompositenameExample1.java

输出

The component at the index position is: yaz
The component at the index position is: klm

示例 2

给出的 Java 程序展示了如何使用 javax.naming 包中的 CompositeName 类来处理由多个部分组成的复合名称。应用程序使用字符串 "aaa/srty/dtfd/f/366/v154+j/enh/d" 初始化 CompositeName 对象,该字符串是分隔符为斜杠的组件序列。通过使用 get() 函数从该复合名称中提取特定组件,结果是位于索引位置 5 和 4 的组件 "v154+j" 和 "366"。

实施

文件名: CompositenameExample2.java

输出

The component at the index position is: v154+j
The component at the index position is: 366