Java中的 CompoundName get() 方法及示例
javax.naming.CompoundName 类的get()方法用于获取此复合名称对象的组件。作为参数传递的位置,用于从复合名称对象中获取出现在该位置的组件。
句法:
public String get(int posn)
参数:此方法接受posn ,它是要检索的组件的从 0 开始的索引。必须在 [0, size()) 范围内。
返回值:此方法返回索引 posn 处的组件。
异常:如果 posn 超出指定范围,此方法将引发ArrayIndexOutOfBoundsException 。
下面的程序说明了 CompoundName.get() 方法:
方案一:
// Java program to demonstrate
// CompoundName.get()
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// need properties for CompoundName
Properties props = new Properties();
props.put("jndi.syntax.separator", ":");
props.put("jndi.syntax.direction",
"left_to_right");
// create compound name object
CompoundName CompoundName1
= new CompoundName("a:b:z:y:x", props);
// apply get()
String pos1 = CompoundName1.get(0);
String pos2 = CompoundName1.get(1);
// print value
System.out.println(pos1);
System.out.println(pos2);
}
}
输出:
a
b
方案二:
// Java program to demonstrate
// CompoundName.get() method
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// need properties for CompoundName
Properties props = new Properties();
props.put("jndi.syntax.separator", "/");
props.put("jndi.syntax.direction",
"left_to_right");
// create compound name object
CompoundName CompoundName1
= new CompoundName(
"c/e/d/v/a/b/z/y/x/f",
props);
// apply get()
String pos3 = CompoundName1.get(3);
String pos4 = CompoundName1.get(4);
// print value
System.out.println("position 3:"
+ pos3);
System.out.println("position 4:"
+ pos4);
}
}
输出:
position 3:v
position 4:a
参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#get(int)